Mais conteúdo relacionado Semelhante a Using PHP 5.3 Namespaces for Fame and Fortune (20) Mais de Zend by Rogue Wave Software (20) Using PHP 5.3 Namespaces for Fame and Fortune1. Using PHP 5.3 Namespaces
for Fame and Fortune
Matthew Weier O'Phinney
Project Lead, Zend Framework
© 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.
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.
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 example
namespace Test;
use FooBarBaz as BarBaz,
FooBaz;
const BAZ = 3;
echo BAZ; // 3
echo BazBAZ; // 2
echo FooBarBAZ; // 1
$baz = new BarBaz(); // FooBarBaz
$baz = new Baz(); // E_FATAL (no matching class)
$baz = new BazBaz(); // FooBazBaz
baz($baz); // E_FATAL (no matching function in
// current namespace)
Bazbaz($baz); // FooBazbaz()
© 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; can't 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; can't
// resolve class
$class = 'FooBarBaz';
if ($o instanceof $class) { } // Success
© 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: we're 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 don't
cost anything
● Helps to document dependencies up front
● Allows static analysis to determine
dependencies
© All rights reserved. Zend Technologies, Inc.
41. ● PHP Manual: http://php.net/namespace
© All rights reserved. Zend Technologies, Inc.
42. Webinar
To watch the recorded webinar, please go to
http://www.zend.com/en/webinar/Framework/
70170000000bWGR-PHP-Namespaces.flv
or
http://bit.ly/pcVMKR
(a short registration is required)
© All rights reserved. Zend Technologies, Inc.