Using PHP 5.3 Namespaces for Fame and Fortune

  • 1,914 views
Uploaded 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 …

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.

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
  • 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.
    Are you sure you want to
    Your message goes here
No Downloads

Views

Total Views
1,914
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
0
Comments
1
Likes
10

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Using PHP 5.3 Namespacesfor Fame and FortuneMatthew Weier OPhinneyProject Lead, Zend Framework © All rights reserved. Zend Technologies, Inc.
  • 2. What are “namespaces”? © All rights reserved. Zend Technologies, Inc.
  • 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. Basics© All rights reserved. Zend Technologies, Inc.
  • 5. Namespace Separator “” Get over it! © All rights reserved. Zend Technologies, Inc.
  • 6. What can live in namespaces● Classes● Constants● Functions © All rights reserved. Zend Technologies, Inc.
  • 7. Declaring namespaces● Single line declaration namespace Foo; © All rights reserved. Zend Technologies, Inc.
  • 8. Declaring namespaces● Block declaration namespace Foo { } namespace Bar { } © All rights reserved. Zend Technologies, Inc.
  • 9. Subnamespaces● Separate the subnamespaces using the namespace separator namespace FooBar; namespace FooBarBaz; © All rights reserved. Zend Technologies, Inc.
  • 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. 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. 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. 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. 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. Importing namespaces namespace Foo; class Bar {} namespace Bar; use Foo; // imports namespace use FooBar; // imports class © All rights reserved. Zend Technologies, Inc.
  • 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. 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. 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. 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. Importing multiple namespaces/classes● Multiple use statements namespace BazBat; use Foo; use Bar; © All rights reserved. Zend Technologies, Inc.
  • 21. Importing multiple namespaces/classes● Or use a comma (“,”) to separate statements namespace BazBat; use Foo, Bar; © All rights reserved. Zend Technologies, Inc.
  • 22. Importing multiple namespaces/classes● With aliases namespace BazBat; use Foo as f, // aliased Bar; // not aliased © All rights reserved. Zend Technologies, Inc.
  • 23. What namespace are you in?● __NAMESPACE__ © All rights reserved. Zend Technologies, Inc.
  • 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. 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. Pitfalls© All rights reserved. Zend Technologies, Inc.
  • 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. 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. 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. Why use namespaces? © All rights reserved. Zend Technologies, Inc.
  • 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. 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. 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. Code Organization: By Responsibility● Takeaway: were referencing the capabilities, not the language type © All rights reserved. Zend Technologies, Inc.
  • 35. Readability● When within a namespace, reference classes directly, keeping usage succinct namespace ZendHttp; $client = new Client(); © All rights reserved. Zend Technologies, Inc.
  • 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. Readability● Aliasing allows giving names context namespace ApplicationController; use ZendControllerAction as ActionController; class FooController extends ActionController {} © All rights reserved. Zend Technologies, Inc.
  • 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. 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. Resources© All rights reserved. Zend Technologies, Inc.
  • 41. ● PHP Manual: http://php.net/namespace © All rights reserved. Zend Technologies, Inc.
  • 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.