Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Namespaces / PSR-0 & PSR-4

Justin Yost
November 06, 2015

Namespaces / PSR-0 & PSR-4

Overview of Namespaces and PSR-0 and PSR-4

Justin Yost

November 06, 2015
Tweet

More Decks by Justin Yost

Other Decks in Programming

Transcript

  1. Namespaces / PSR 0 & 4 Justin Yost Web Developer

    at Loadsys » twitter.com/jtyost2 » github.com/jtyost2 » lynda.com/justinyost » yostivanich.com CC BY-NC 4.0 Justin Yost 1
  2. Problems » Solves collisions between files and class names »

    Ability to alias CC BY-NC 4.0 Justin Yost 3
  3. <?php namespace Awesome; class EmpireStrikesBack {} ?> <?php namespace Favorites;

    class EmpireStrikesBack {} ?> CC BY-NC 4.0 Justin Yost 6
  4. Namespaces Let you create a space for your code to

    live without naming conflicts with other code. * Classes * Functions * CONST * Interfaces CC BY-NC 4.0 Justin Yost 7
  5. Ok why do I care? Ever named a class? *

    User * Auth * Login * Table * Database * String * ... CC BY-NC 4.0 Justin Yost 12
  6. How to use? » Declare namespaces » Reference classes by

    their namespaced name CC BY-NC 4.0 Justin Yost 13
  7. file2.php <?php namespace Favorites; include 'file1.php'; class EmpireStrikesBack {} new

    EmpireStrikesBack(); // \Favorites\EmpireStrikesBack() new Awesome\EmpireStrikesBack(); // \Favorites\Awesome\EmpireStrikesBack() new \Awesome\EmpireStrikesBack(); // \Awesome\EmpireStrikesBack() new \Exception(); // \Exception ?> CC BY-NC 4.0 Justin Yost 16
  8. Aliasing/Importing » use imports a namespace » use also lets

    you alias a Namespaced object CC BY-NC 4.0 Justin Yost 17
  9. <?php namespace Favorites; use Awesome\EmpireStrikesBack as Something; use Awesome\LaptiNeck; class

    EmpireStrikesBack {} new EmpireStrikesBack(); // \Favorites\EmpireStrikesBack() new Awesome\EmpireStrikesBack(); // \Favorites\Awesome\EmpireStrikesBack() new \Awesome\EmpireStrikesBack(); // \Awesome\EmpireStrikesBack() new Something(); // \Awesome\EmpireStrikesBack() new LaptiNeck(); // \Awesome\LaptiNeck() new \Exception(); // \Exception ?> CC BY-NC 4.0 Justin Yost 19
  10. Must I use Namespaces » Nope » But really, you

    should be CC BY-NC 4.0 Justin Yost 20
  11. PSR-0 » Paths match the Namespace Path » And other

    stuff... \Doctrine\Common\IsolatedClassLoader => /path/to/project/lib/vendor/Doctrine/Common/IsolatedClassLoader.php CC BY-NC 4.0 Justin Yost 22
  12. PSR-0 » Deprecated » Out with the old and all

    that » There were some issues » Composer, Underscores, etc CC BY-NC 4.0 Justin Yost 23
  13. PSR-4 » \<NamespaceName>(\<SubNamespaceNames>)*\<ClassName> » Must include vendor Namespace » May

    include sub namespaces \Symfony\Core\Request => /path/to/project/lib/vendor/Symfony/Core/Request.php CC BY-NC 4.0 Justin Yost 24
  14. PSR-4 » Focused on packages » Underscores have no meaning

    » Composer supports both PSR-4/0 CC BY-NC 4.0 Justin Yost 25
  15. Composer briefly » Composer gives you a way of assigning

    dependencies » Includes an autoloader to load said dependencies » Pretty much every framework supports/requires composer » Composer includes pretty good information on using it without a framework CC BY-NC 4.0 Justin Yost 26
  16. Autloading? » Include Composer's Autoloader File » And then just

    reference namespaced Composer Files CC BY-NC 4.0 Justin Yost 27
  17. <?php namespace Favorites; require __DIR__ . '/vendor/autoload.php'; use Awesome\LaptiNeck; class

    EmpireStrikesBack {} new EmpireStrikesBack(); // \Favorites\EmpireStrikesBack() new Awesome\EmpireStrikesBack(); // \Favorites\Awesome\EmpireStrikesBack() new \Awesome\EmpireStrikesBack(); // \Awesome\EmpireStrikesBack() new LaptiNeck(); // \Awesome\LaptiNeck() new \Exception(); // \Exception ?> CC BY-NC 4.0 Justin Yost 29
  18. What Should I Have Taken Away » Namespaces solve a

    real problem » Namespaces are actually relatively easy » PSR-4 doesn't require a lot of work » Implement composer and you solve several problems CC BY-NC 4.0 Justin Yost 30