Using PHP 5.3 Namespacesfor Fame and FortuneMatthew Weier OPhinneyProject Lead, Zend Framework                            ...
What are “namespaces”?     © All rights reserved. Zend Technologies, Inc.
What are “namespaces”?                 "A way in which to group related classes, functions and constants"                 ...
Basics© All rights reserved. Zend Technologies, Inc.
Namespace Separator                  “”                                                                Get over it!       ...
What can live in namespaces●   Classes●   Constants●   Functions                © All rights reserved. Zend Technologies, ...
Declaring namespaces●   Single line declaration    namespace Foo;                     © All rights reserved. Zend Technolo...
Declaring namespaces●   Block declaration    namespace Foo    {    }    namespace Bar    {    }                    © All r...
Subnamespaces●   Separate the subnamespaces using the    namespace separator    namespace FooBar;    namespace FooBarBaz; ...
Referring to namespaced elements●   From non-namespaced code:    $class = new FooBarClass();    $class = new FooBarBazClas...
Referring to namespaced elements●   From code using the same namespace:    namespace Foo;    $class = new BarClass();    $...
Referring to namespaced elements●   From code in a different namespace:    namespace Bar;    $class = new FooBarClass();  ...
Using namespaced code:●   Prefixing with a namespace separator resolves    as a fully qualified name●   Resolution order: ...
Importing namespaces●   A way to bring code from another namespace    into the current namespace.●   Import:    ▶   Classe...
Importing namespaces  namespace Foo;  class Bar {}  namespace Bar;  use Foo;     // imports namespace  use FooBar; // impo...
Using imported code  namespace Bar;  use Foo;     // imports namespace  $bar = new FooBar();  namespace Bar;  use FooBar; ...
Aliasing●   "import namespace or class, but refer to it    using this name"●   PHP utilizes the as keyword to alias       ...
Aliasing  namespace Bar;  use Foo as f;  $bar = new fBar();  namespace Bar;  use FooBar as bar;  $bar = new bar();        ...
Make typehinting more semantic!namespace App;use ZendEventManagerEventManager as Events;class Foo{    public function even...
Importing multiple namespaces/classes●   Multiple use statements    namespace BazBat;    use Foo;    use Bar;             ...
Importing multiple namespaces/classes●   Or use a comma (“,”) to separate statements    namespace BazBat;    use Foo,     ...
Importing multiple namespaces/classes●   With aliases    namespace BazBat;    use Foo as f, // aliased        Bar;      //...
What namespace are you in?●   __NAMESPACE__                © All rights reserved. Zend Technologies, Inc.
Comprehensive example●   The setup:    namespace FooBar;    const BAZ = 1;    function baz($data) {}    class Baz {}    na...
Comprehensive examplenamespace Test;use FooBarBaz as BarBaz,    FooBaz;const BAZ = 3;echo BAZ;          // 3echo BazBAZ;  ...
Pitfalls© All rights reserved. Zend Technologies, Inc.
Referencing namespaced code in strings●   Classes, constants, and functions referenced in    a string MUST be fully qualif...
Referencing namespaced code in strings●   Example 1    namespace Foo;    // Assume FooBarBaz is a defined class    $class ...
Referencing namespaced code in strings●   Example 2    namespace Foo;    // Assume FooBarBaz is a defined class    $class ...
Why use namespaces?    © All rights reserved. Zend Technologies, Inc.
Code Organization: Filesystem●   Namespace separator has an affinity for the    directory separator●   Suggests a 1:1 rela...
Code Organization: By Responsibility●   Interfaces    ▶   Use cases:         ●   instanceof: $class instanceof Adapter    ...
Code Organization: By Responsibility    namespace ComponentAdapter;    use ComponentAdapter;    class ConcreteAdapter impl...
Code Organization: By Responsibility●   Takeaway: were referencing the capabilities,    not the language type             ...
Readability●   When within a namespace, reference classes    directly, keeping usage succinct    namespace ZendHttp;    $c...
Readability●   Importing and aliasing make names more    succinct    namespace Application;    use ZendHttpClient as HttpC...
Readability●   Aliasing allows giving names context    namespace ApplicationController;    use ZendControllerAction       ...
Dependency declarations●   Suggestion: import every class outside the    current namespace that you consume    namespace A...
Dependency declarations●   Imports are hints to the interpreter, and dont    cost anything●   Helps to document dependenci...
Resources© All rights reserved. Zend Technologies, Inc.
●   PHP Manual: http://php.net/namespace                   © All rights reserved. Zend Technologies, Inc.
WebinarTo watch the recorded webinar, please go tohttp://www.zend.com/en/webinar/Framework/70170000000bWGR-PHP-Namespaces....
Upcoming SlideShare
Loading in...5
×

Using PHP 5.3 Namespaces for Fame and Fortune

2,022

Published on

This is the slide deck of the Zend webinar "Using PHP 5.3 Namespaces for Fame and Fortune". In this webinar, Matthew Weier O'Phinney looks at the basics of using namespaces, some strategies for organizing your code to use namespaces effectively, and some tips and tricks for using namespaced and non-namespaced code together. A recording of this webinar can be watched at http://bit.ly/pcVMKR, after a short registration.

Published in: Technology
1 Comment
10 Likes
Statistics
Notes
  • This is the slide deck of the Zend webinar 'Using PHP 5.3 Namespaces for Fame and Fortune'.

    You can watch the related webinar at http://bit.ly/pcVMKR, after a short registration.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
2,022
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
1
Likes
10
Embeds 0
No embeds

No notes for slide

Transcript of "Using PHP 5.3 Namespaces for Fame and Fortune"

  1. 1. Using PHP 5.3 Namespacesfor Fame and FortuneMatthew Weier OPhinneyProject Lead, Zend Framework © All rights reserved. Zend Technologies, Inc.
  2. 2. What are “namespaces”? © All rights reserved. Zend Technologies, Inc.
  3. 3. What are “namespaces”? "A way in which to group related classes, functions and constants" – http://php.net/namespace © All rights reserved. Zend Technologies, Inc.
  4. 4. Basics© All rights reserved. Zend Technologies, Inc.
  5. 5. Namespace Separator “” Get over it! © All rights reserved. Zend Technologies, Inc.
  6. 6. What can live in namespaces● Classes● Constants● Functions © All rights reserved. Zend Technologies, Inc.
  7. 7. Declaring namespaces● Single line declaration namespace Foo; © All rights reserved. Zend Technologies, Inc.
  8. 8. Declaring namespaces● Block declaration namespace Foo { } namespace Bar { } © All rights reserved. Zend Technologies, Inc.
  9. 9. Subnamespaces● Separate the subnamespaces using the namespace separator namespace FooBar; namespace FooBarBaz; © All rights reserved. Zend Technologies, Inc.
  10. 10. Referring to namespaced elements● From non-namespaced code: $class = new FooBarClass(); $class = new FooBarBazClass(); FoobarFunc(); // function call $val = FooBAT; // constant value © All rights reserved. Zend Technologies, Inc.
  11. 11. Referring to namespaced elements● From code using the same namespace: namespace Foo; $class = new BarClass(); $class = new BarBazClass(); barFunc(); // function call $val = BAT; // constant value © All rights reserved. Zend Technologies, Inc.
  12. 12. Referring to namespaced elements● From code in a different namespace: namespace Bar; $class = new FooBarClass(); $class = new FooBarBazClass(); FoobarFunc(); // function call $val = FooBAT; // constant value © All rights reserved. Zend Technologies, Inc.
  13. 13. Using namespaced code:● Prefixing with a namespace separator resolves as a fully qualified name● Resolution order: ▶ Globally qualified: known; otherwise: ▶ Current namespace, or relative to it (Classes, functions, constants) ▶ Global namespace (functions, constants) © All rights reserved. Zend Technologies, Inc.
  14. 14. Importing namespaces● A way to bring code from another namespace into the current namespace.● Import: ▶ Classes ▶ Other namespaces● Keyword used is use © All rights reserved. Zend Technologies, Inc.
  15. 15. Importing namespaces namespace Foo; class Bar {} namespace Bar; use Foo; // imports namespace use FooBar; // imports class © All rights reserved. Zend Technologies, Inc.
  16. 16. Using imported code namespace Bar; use Foo; // imports namespace $bar = new FooBar(); namespace Bar; use FooBar; // imports class $bar = new Bar(); © All rights reserved. Zend Technologies, Inc.
  17. 17. Aliasing● "import namespace or class, but refer to it using this name"● PHP utilizes the as keyword to alias © All rights reserved. Zend Technologies, Inc.
  18. 18. Aliasing namespace Bar; use Foo as f; $bar = new fBar(); namespace Bar; use FooBar as bar; $bar = new bar(); © All rights reserved. Zend Technologies, Inc.
  19. 19. Make typehinting more semantic!namespace App;use ZendEventManagerEventManager as Events;class Foo{ public function events(Events $events = null) { if ($events instanceof Events) { $this->events = $events; } elseif (!$this->events instanceof Events) { $this->events = new Events(__CLASS__); } return $this->events; }} © All rights reserved. Zend Technologies, Inc.
  20. 20. Importing multiple namespaces/classes● Multiple use statements namespace BazBat; use Foo; use Bar; © All rights reserved. Zend Technologies, Inc.
  21. 21. Importing multiple namespaces/classes● Or use a comma (“,”) to separate statements namespace BazBat; use Foo, Bar; © All rights reserved. Zend Technologies, Inc.
  22. 22. Importing multiple namespaces/classes● With aliases namespace BazBat; use Foo as f, // aliased Bar; // not aliased © All rights reserved. Zend Technologies, Inc.
  23. 23. What namespace are you in?● __NAMESPACE__ © All rights reserved. Zend Technologies, Inc.
  24. 24. Comprehensive example● The setup: namespace FooBar; const BAZ = 1; function baz($data) {} class Baz {} namespace FooBaz; const BAZ = 2; function baz($data) {} class Baz {} © All rights reserved. Zend Technologies, Inc.
  25. 25. Comprehensive examplenamespace Test;use FooBarBaz as BarBaz, FooBaz;const BAZ = 3;echo BAZ; // 3echo BazBAZ; // 2echo FooBarBAZ; // 1$baz = new BarBaz(); // FooBarBaz$baz = new Baz(); // E_FATAL (no matching class)$baz = new BazBaz(); // FooBazBazbaz($baz); // E_FATAL (no matching function in // current namespace)Bazbaz($baz); // FooBazbaz() © All rights reserved. Zend Technologies, Inc.
  26. 26. Pitfalls© All rights reserved. Zend Technologies, Inc.
  27. 27. Referencing namespaced code in strings● Classes, constants, and functions referenced in a string MUST be fully qualified● No namespace separator prefix is necessary © All rights reserved. Zend Technologies, Inc.
  28. 28. Referencing namespaced code in strings● Example 1 namespace Foo; // Assume FooBarBaz is a defined class $class = BarBaz; $o = new $class; // E_FATAL; cant find // relative names $class = FooBarBaz; $o = new $class; // Success $class = FooBarBaz; $o = new $class; // Also success, // but not necessary © All rights reserved. Zend Technologies, Inc.
  29. 29. Referencing namespaced code in strings● Example 2 namespace Foo; // Assume FooBarBaz is a defined class $class = BarBaz; if ($o instanceof $class) { } // Fails; cant // resolve class $class = FooBarBaz; if ($o instanceof $class) { } // Success © All rights reserved. Zend Technologies, Inc.
  30. 30. Why use namespaces? © All rights reserved. Zend Technologies, Inc.
  31. 31. Code Organization: Filesystem● Namespace separator has an affinity for the directory separator● Suggests a 1:1 relationship with file system namespace FooBar; class Baz {} /* Foo/Bar/Baz.php (*nix) FooBarBaz.php (Windows) */ © All rights reserved. Zend Technologies, Inc.
  32. 32. Code Organization: By Responsibility● Interfaces ▶ Use cases: ● instanceof: $class instanceof Adapter ● implements: SomeClass implements Adapter ▶ Natural language is easiest to understand ▶ Natural language suggests a hierarchy © All rights reserved. Zend Technologies, Inc.
  33. 33. Code Organization: By Responsibility namespace ComponentAdapter; use ComponentAdapter; class ConcreteAdapter implements Adapter {}● interface ComponentAdapter in Component/Adapter.php● class ComponentAdapterConcreteAdapter in Component/Adapter/ConcreteAdapter.php © All rights reserved. Zend Technologies, Inc.
  34. 34. Code Organization: By Responsibility● Takeaway: were referencing the capabilities, not the language type © All rights reserved. Zend Technologies, Inc.
  35. 35. Readability● When within a namespace, reference classes directly, keeping usage succinct namespace ZendHttp; $client = new Client(); © All rights reserved. Zend Technologies, Inc.
  36. 36. Readability● Importing and aliasing make names more succinct namespace Application; use ZendHttpClient as HttpClient, ZendLoggerLogger; $client = new HttpClient(); $log = new Logger(); © All rights reserved. Zend Technologies, Inc.
  37. 37. Readability● Aliasing allows giving names context namespace ApplicationController; use ZendControllerAction as ActionController; class FooController extends ActionController {} © All rights reserved. Zend Technologies, Inc.
  38. 38. Dependency declarations● Suggestion: import every class outside the current namespace that you consume namespace ApplicationController; use ZendControllerAction as ActionController, MyORMMapper as DataMapper, MyEntityBlogEntry, ZendEventManagerEventManager; © All rights reserved. Zend Technologies, Inc.
  39. 39. Dependency declarations● Imports are hints to the interpreter, and dont cost anything● Helps to document dependencies up front● Allows static analysis to determine dependencies © All rights reserved. Zend Technologies, Inc.
  40. 40. Resources© All rights reserved. Zend Technologies, Inc.
  41. 41. ● PHP Manual: http://php.net/namespace © All rights reserved. Zend Technologies, Inc.
  42. 42. WebinarTo watch the recorded webinar, please go tohttp://www.zend.com/en/webinar/Framework/70170000000bWGR-PHP-Namespaces.flvorhttp://bit.ly/pcVMKR(a short registration is required) © All rights reserved. Zend Technologies, Inc.

×