2011-03-02-namespaces-v2

Upload: sederhana-gulo

Post on 06-Apr-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 2011-03-02-Namespaces-v2

    1/84

    All rights reserved. Zend Technolo

    Using PHP 5.3 Namespacesfor Fame and Fortune

    Matthew Weier O'PhinneyProject Lead, Zend Framework

  • 8/3/2019 2011-03-02-Namespaces-v2

    2/84

    All rights reserved. Zend Technologies, Inc.

    What are namespaces?

  • 8/3/2019 2011-03-02-Namespaces-v2

    3/84

    All rights reserved. Zend Technologies, Inc.

    What are namespaces?

    "A way in which to grouprelated classes, functions and constants

    http://php.net/namespace

  • 8/3/2019 2011-03-02-Namespaces-v2

    4/84

    All rights reserved. Zend Technologies, Inc.

    Basics

  • 8/3/2019 2011-03-02-Namespaces-v2

    5/84

    All rights reserved. Zend Technologies, Inc.

    Namespace Separator

    \

    Get over it!

  • 8/3/2019 2011-03-02-Namespaces-v2

    6/84

    All rights reserved. Zend Technologies, Inc.

    What can live in namespaces

    Classes

    Constants Functions

  • 8/3/2019 2011-03-02-Namespaces-v2

    7/84

    All rights reserved. Zend Technologies, Inc.

    Declaring namespaces

    Single line declaration

    namespace Zend;namespace Zend;

  • 8/3/2019 2011-03-02-Namespaces-v2

    8/84

    All rights reserved. Zend Technologies, Inc.

    Declaring namespaces

    Block declaration

    namespace Zend{}namespace Phly{}

    namespace Zend{}namespacePhly{}

  • 8/3/2019 2011-03-02-Namespaces-v2

    9/84

    All rights reserved. Zend Technologies, Inc.

    Subnamespaces

    Separate the subnamespaces using thenamespace separator

    namespace Zend\Log;

    namespace Zend\Log\Writer;

    namespace Zend\Log;

    namespace Zend\Log\Writer;

  • 8/3/2019 2011-03-02-Namespaces-v2

    10/84

    All rights reserved. Zend Technologies, Inc.

    Referring to namespaced elements

    From non-namespaced code:

    $class = new My\Registry();$class = new My\Log\Logger();My\str_split($s); // function call$val = My\APIKEY; // constant value

    $class = new My\Registry();$class=new My\Log\Logger();My\str_split($s);// function call$val= My\APIKEY;// constant value

  • 8/3/2019 2011-03-02-Namespaces-v2

    11/84

    All rights reserved. Zend Technologies, Inc.

    Referring to namespaced elements

    From code using the same namespace:

    namespace My;

    $class = new Registry();$class = new Log\Logger();str_split($s); // function call$val = APIKEY; // constant value

    namespace My;

    $class=new Registry();$class=new Log\Logger();str_split($s);// function call$val= APIKEY;// constant value

  • 8/3/2019 2011-03-02-Namespaces-v2

    12/84

    All rights reserved. Zend Technologies, Inc.

    Referring to namespaced elements

    From code in a different namespace:

    namespace Your;

    $class = new \My\Registry();$class = new \My\Log\Logger();\My\str_split($s); // function call$val = \My\APIKEY; // constant value

    namespace Your;

    $class=new\My\Registry();$class=new\My\Log\Logger();\My\str_split($s);// function call$val=\My\APIKEY;// constant value

  • 8/3/2019 2011-03-02-Namespaces-v2

    13/84

    All rights reserved. Zend Technologies, Inc.

    Using namespaced code:

    Prefixing with a namespace separator resolvesas a fully qualified name

    Resolution order:

    Globally qualified: known; otherwise:

    Current namespace, or relative to it(Classes, functions, constants)

    Global namespace(functions, constants)

  • 8/3/2019 2011-03-02-Namespaces-v2

    14/84

    All rights reserved. Zend Technologies, Inc.

    This means:

    You can override system functions withinnamespaces!

    namespace My;

    function str_split($string, $splitlen = 2){ return preg_split('/\W/', $string, $splitlen);}

    $a = str_split("Foo, Bar"); // invokes My\str_split/* array (* 0 => 'Foo',* 1 => ' Bar',* )*/

    namespace My;

    functionstr_split($string,$splitlen=2){ returnpreg_split('/\W/',$string,$splitlen);}

    $a=str_split("Foo, Bar");// invokes My\str_split(/* array (* 0 => 'Foo',* 1 => ' Bar',* )*/

  • 8/3/2019 2011-03-02-Namespaces-v2

    15/84

    All rights reserved. Zend Technologies, Inc.

    Importing namespaces

    A way to bring code from another namespaceinto the current namespace.

    Import:

    Classes

    Other namespaces

    Keyword used is use

  • 8/3/2019 2011-03-02-Namespaces-v2

    16/84

    All rights reserved. Zend Technologies, Inc.

    Importing namespaces

    namespace My;

    class Registry {}

    namespace Your;use My; // imports namespaceuse My\Registry; // imports class

    namespace My;

    classRegistry{}

    namespace Your;use My; // imports namespaceuse My\Registry;// imports class

  • 8/3/2019 2011-03-02-Namespaces-v2

    17/84

    All rights reserved. Zend Technologies, Inc.

    Using imported code

    namespace Your;

    use My; // imports namespace$r = new My\Registry();

    namespace Your;use My\Registry; // imports class$r = new Registry();

    namespace Your;use

    My; // imports namespace$r=new My\Registry();

    namespace Your;use My\Registry;// imports class$r=new Registry();

  • 8/3/2019 2011-03-02-Namespaces-v2

    18/84

    All rights reserved. Zend Technologies, Inc.

    Aliasing

    "import namespace or class, but refer to itusing this name"

    PHP utilizes the as keyword to alias

  • 8/3/2019 2011-03-02-Namespaces-v2

    19/84

    All rights reserved. Zend Technologies, Inc.

    Aliasing

    namespace Your;

    use My as m;$r = new m\Registry();

    namespace Your;use My\Registry as reg;$r = new reg();

    namespace Your;use

    Myas

    m;$r=new m\Registry();

    namespace Your;use My\Registryas reg;$r=new reg();

  • 8/3/2019 2011-03-02-Namespaces-v2

    20/84

    All rights reserved. Zend Technologies, Inc.

    Make typehinting more semantic!

    namespace App;

    use Zend\EventManager\EventManager as Events;

    class Foo{ public function events(Events $events = null)

    { if ($eventsinstanceof Events) { $this->events = $events;

    } elseif (!$this->events instanceof Events) $this->events = new Events(__CLASS__);}

    return$this->events;}

    }

    namespace App;useZend\EventManager\EventManagerasEvents;

    classFoo{ public functionevents(Events$events=null) { if($eventsinstanceofEvents) { $this->events=$events; }elseif(!$this->eventsinstanceofEvents) { $this->events=newEvents(__CLASS__); } return$this->events; }}

  • 8/3/2019 2011-03-02-Namespaces-v2

    21/84

    All rights reserved. Zend Technologies, Inc.

    Importing multiple namespaces/classes

    Multiple use statements

    namespace Their;use My;use Your;

    namespace Their;use My;use Your;

  • 8/3/2019 2011-03-02-Namespaces-v2

    22/84

    All rights reserved. Zend Technologies, Inc.

    Importing multiple namespaces/classes

    Or use a comma (,) to separate statements

    namespace Their;use My,

    Your;

    namespace Their;use My,

    Your;

  • 8/3/2019 2011-03-02-Namespaces-v2

    23/84

    All rights reserved. Zend Technologies, Inc.

    Importing multiple namespaces/classes

    With aliases

    namespace Their;

    use My as m, // aliasedYour; // not aliased

    namespace Their;

    useMyas m,// aliasedYour; // not aliased

  • 8/3/2019 2011-03-02-Namespaces-v2

    24/84

    All rights reserved. Zend Technologies, Inc.

    What namespace are you in?

    __NAMESPACE__

  • 8/3/2019 2011-03-02-Namespaces-v2

    25/84

    All rights reserved. Zend Technologies, Inc.

    Comprehensive example

    namespace My\Package;const APIKEY = 1;function get($data) {}class Service {}

    namespace My\OtherPackage;const APIKEY = 2;

    function get($data) {}class Service {}

    namespace My\Package;constAPIKEY=1;function get($data) {}classService{}

    namespace My\OtherPackage;constAPIKEY=2;

    function get($data) {}classService{}

    The setup:

  • 8/3/2019 2011-03-02-Namespaces-v2

    26/84

    All rights reserved. Zend Technologies, Inc.

    Comprehensive example

    namespace Test;use My\Package\Service as PackageService,

    My\OtherPackage;const APIKEY = 3;echo APIKEY; // 3echo OtherPackage\APIKEY; // 2echo \My\Package\APIKEY; // 1

    $s = new PackageService(); // My\Package\Service

    $s = new Service(); // E_FATAL (no match)$s = new OtherPackage\Service();// My\OtherPackage\Service

    get($baz); // E_FATAL (no matching function in// current namespace)

    OtherPackage\get($baz); // My\OtherPackage\get()

    namespace Test;use My\Package\Serviceas PackageService,

    My\OtherPackage;constAPIKEY=3;echo APIKEY; // 3echo OtherPackage\APIKEY;// 2echo\My\Package\APIKEY; // 1

    $s=new PackageService();// My\Package\Service$s=new Service(); // E_FATAL (no match)$s=new OtherPackage\Service();

    // My\OtherPackage\Service

    get($baz); // E_FATAL (no matching function in // current namespace)OtherPackage\get($baz);// My\OtherPackage\get()

  • 8/3/2019 2011-03-02-Namespaces-v2

    27/84

    All rights reserved. Zend Technologies, Inc.

    Pitfalls

  • 8/3/2019 2011-03-02-Namespaces-v2

    28/84

    All rights reserved. Zend Technologies, Inc.

    Referencing namespaced code in strings

    Classes, constants, and functions referenced ina string MUSTbe fully qualified

    No namespace separatorprefixis necessary

  • 8/3/2019 2011-03-02-Namespaces-v2

    29/84

    All rights reserved. Zend Technologies, Inc.

    Referencing namespaced code in strings

    Example 1

    namespace My;// Assume My\Log\Logger is a defined class

    $class = 'Log\Logger';$o = new$class; // E_FATAL; can't find

    // relative names

    $class = 'My\Log\Logger';$o = new$class; // Success

    $class = '\My\Log\Logger';$o = new$class; // Also success,

    // but not necessary

    namespace My;// Assume My\Log\Logger is a defined class

    $class='Log\Logger';$o =new$class;// E_FATAL; can't find

    // relative names

    $class='My\Log\Logger';$o =new$class;// Success

    $class='\My\Log\Logger';$o =new$class;// Also success,

    // but not necessary

  • 8/3/2019 2011-03-02-Namespaces-v2

    30/84

    All rights reserved. Zend Technologies, Inc.

    Referencing namespaced code in strings

    Example 2

    namespace My;// Assume My\Log\Logger is a defined class

    $class = 'Log\Logger';if ($oinstanceof$class) { } // Fails; can't

    // resolve class

    $class = 'My\Log\Logger';if ($oinstanceof$class) { } // Success

    namespace My;// Assume My\Log\Logger is a defined class

    $class='Log\Logger';if($oinstanceof$class) { }// Fails; can't

    // resolve class

    $class='My\Log\Logger';if($oinstanceof$class) { }// Success

  • 8/3/2019 2011-03-02-Namespaces-v2

    31/84

    All rights reserved. Zend Technologies, Inc.

    Why use namespaces?

  • 8/3/2019 2011-03-02-Namespaces-v2

    32/84

    All rights reserved. Zend Technologies, Inc.

    Code Organization: Filesystem

    Namespace separator has an affinity for thedirectory separator

    Suggests a 1:1 relationship with file system

    namespace My\Log;class Logger {}/* My/Log/Logger.php (*nix)

    My\Log\Logger.php (Windows) */

    namespace My\Log;classLogger{}/* My/Log/Logger.php (*nix)

    My\Log\Logger.php (Windows) */

  • 8/3/2019 2011-03-02-Namespaces-v2

    33/84

    All rights reserved. Zend Technologies, Inc.

    Code Organization: By Responsibility

    Interfaces

    Use cases: i nsta nceof : $ c l a s s i n s t a n c e o f Ad a p t e r

    i m pl e m ents : S o m e C l a s s i m p l e m e n t s Ad a p t e r

    Natural language is easiest to understand

    Natural language suggests a hierarchy

  • 8/3/2019 2011-03-02-Namespaces-v2

    34/84

    All rights reserved. Zend Technologies, Inc.

    Code Organization: By Responsibility

    interfaceT r a n s l a t o r \ Ad a p t e r

    inT r a n s l a t o r / A d a p t e r . p h p

    class T r a n s l a t o r \ Ad a p t e r \ Co n c r e t e A d a p t e r inT r a n s l a t o r / A d a p t e r / C o n c r e t e A d a p t e r . p h p

    namespace Translator\Adapter;

    use Translator\Adapter;

    class ConcreteAdapter implements Adapter{}

    namespace Translator\Adapter;

    use Translator\Adapter;

    classConcreteAdapterimplementsAdapter{}

  • 8/3/2019 2011-03-02-Namespaces-v2

    35/84

    All rights reserved. Zend Technologies, Inc.

    Code Organization: By Responsibility

    Takeaway: we're referencing the capabilities,not the language type

  • 8/3/2019 2011-03-02-Namespaces-v2

    36/84

    All rights reserved. Zend Technologies, Inc.

    Readability

    When within a namespace, reference classesdirectly, keeping usage succinct

    namespace Zend\Http;$client = new Client();

    namespace Zend\Http;$client=newClient();

  • 8/3/2019 2011-03-02-Namespaces-v2

    37/84

    All rights reserved. Zend Technologies, Inc.

    Readability

    Importing and aliasing make names moresuccinct

    namespace Application;use Zend\Http\Client as HttpClient,

    Zend\Logger\Logger;

    $client = new HttpClient();$log = new Logger();

    namespace Application;useZend\Http\ClientasHttpClient,

    Zend\Logger\Logger;

    $client=newHttpClient();$log =newLogger();

  • 8/3/2019 2011-03-02-Namespaces-v2

    38/84

    All rights reserved. Zend Technologies, Inc.

    Readability

    Aliasing allows giving names context

    namespace Application\Controller;

    use Zend\Controller\Actionas ActionController;

    class FooControllerextends ActionController {}

    namespace Application\Controller;

    useZend\Controller\ActionasActionController;

    classFooControllerextendsActionController{}

  • 8/3/2019 2011-03-02-Namespaces-v2

    39/84

    All rights reserved. Zend Technologies, Inc.

    Dependency declarations

    Suggestion: import everyclass outside thecurrent namespace that you consume

    namespace Application\Controller;

    use Zend\Controller\Actionas ActionController,

    My\ORM\Mapper as DataMapper,My\Entity\BlogEntry,Zend\EventManager\EventManager;

    namespace Application\Controller;

    useZend\Controller\ActionasActionController,

    My\ORM\MapperasDataMapper,My\Entity\BlogEntry,

    Zend\EventManager\EventManager;

  • 8/3/2019 2011-03-02-Namespaces-v2

    40/84

    All rights reserved. Zend Technologies, Inc.

    Dependency declarations

    Imports are hints to the interpreter, and don'tcost anything

    Helps to document dependencies up front

    Allows static analysis to determinedependencies

  • 8/3/2019 2011-03-02-Namespaces-v2

    41/84

    All rights reserved. Zend Technologies, Inc.

    Resources

  • 8/3/2019 2011-03-02-Namespaces-v2

    42/84

    All rights reserved. Zend Technologies, Inc.

    PHP Manual: http://php.net/namespace

  • 8/3/2019 2011-03-02-Namespaces-v2

    43/84

  • 8/3/2019 2011-03-02-Namespaces-v2

    44/84

  • 8/3/2019 2011-03-02-Namespaces-v2

    45/84

    Basically, a language-supported mechanism forgrouping these language features, as well as alanguage-supported mechanism for avoiding naming

    conflicts.

  • 8/3/2019 2011-03-02-Namespaces-v2

    46/84

  • 8/3/2019 2011-03-02-Namespaces-v2

    47/84

    All rights reserved. Zend Technologies, Inc.

    Namespace Separator

    \

    Get over it!

    There are a ton of reasons why the backslash wasused, ranging from length of token to position of thetoken on most keyboards to previous use of other

    tokens to understanding the scope of a givenoperator/operation visually. Just use it, and movealong.

  • 8/3/2019 2011-03-02-Namespaces-v2

    48/84

    All rights reserved. Zend Technologies, Inc.

    What can live in namespaces

    Classes

    Constants Functions

  • 8/3/2019 2011-03-02-Namespaces-v2

    49/84

    All rights reserved. Zend Technologies, Inc.

    Declaring namespaces

    Single line declaration

    namespace Zend;namespace Zend;

    You _can_ declare multiple namespaces in the samefile in this way, but it's not recommended

  • 8/3/2019 2011-03-02-Namespaces-v2

    50/84

    All rights reserved. Zend Technologies, Inc.

    Declaring namespaces

    Block declaration

    namespace Zend{}namespace Phly{}

    namespace Zend{}namespacePhly{}

    This is the recommended way to declare multiplenamespaces when in a single file.

  • 8/3/2019 2011-03-02-Namespaces-v2

    51/84

    All rights reserved. Zend Technologies, Inc.

    Subnamespaces

    Separate the subnamespaces using the

    namespace separator

    namespace Zend\Log;

    namespace Zend\Log\Writer;

    namespace Zend\Log;

    namespace Zend\Log\Writer;

  • 8/3/2019 2011-03-02-Namespaces-v2

    52/84

    All rights reserved. Zend Technologies, Inc.

    Referring to namespaced elements

    From non-namespaced code:

    $class = new My\Registry();$class = new My\Log\Logger();My\str_split($s); // function call$val = My\APIKEY; // constant value

    $class = new My\Registry();$class=new My\Log\Logger();My\str_split($s);// function call$val= My\APIKEY;// constant value

  • 8/3/2019 2011-03-02-Namespaces-v2

    53/84

    All rights reserved. Zend Technologies, Inc.

    Referring to namespaced elements

    From code using the same namespace:

    namespace My;

    $class = new Registry();$class = new Log\Logger();str_split($s); // function call$val = APIKEY; // constant value

    namespace My;

    $class=new Registry();$class=new Log\Logger();str_split($s);// function call$val= APIKEY;// constant value

  • 8/3/2019 2011-03-02-Namespaces-v2

    54/84

    All rights reserved. Zend Technologies, Inc.

    Referring to namespaced elements

    From code in a different namespace:

    namespace Your;

    $class = new \My\Registry();$class = new \My\Log\Logger();\My\str_split($s); // function call$val = \My\APIKEY; // constant value

    namespace Your;

    $class=new\My\Registry();$class=new\My\Log\Logger();\My\str_split($s);// function call$val=\My\APIKEY;// constant value

  • 8/3/2019 2011-03-02-Namespaces-v2

    55/84

  • 8/3/2019 2011-03-02-Namespaces-v2

    56/84

  • 8/3/2019 2011-03-02-Namespaces-v2

    57/84

    All rights reserved. Zend Technologies, Inc.

    Importing namespaces

    A way to bring code from another namespace

    into the current namespace. Import:

    Classes

    Other namespaces

    Keyword used is use

  • 8/3/2019 2011-03-02-Namespaces-v2

    58/84

    All rights reserved. Zend Technologies, Inc.

    Importing namespaces

    namespace My;class Registry {}

    namespace Your;use My; // imports namespaceuse My\Registry; // imports class

    namespace My;

    classRegistry{}

    namespace Your;use My; // imports namespaceuse My\Registry;// imports class

    Note: importing top-level namespaces in code with nonamespace has no effect and results in a warning.

  • 8/3/2019 2011-03-02-Namespaces-v2

    59/84

    All rights reserved. Zend Technologies, Inc.

    Using imported code

    namespace Your;use My; // imports namespace$r = new My\Registry();

    namespace Your;use My\Registry; // imports class$r = new Registry();

    namespace Your;

    use My; // imports namespace$r=new My\Registry();

    namespace Your;use My\Registry;// imports class$r=new Registry();

  • 8/3/2019 2011-03-02-Namespaces-v2

    60/84

    All rights reserved. Zend Technologies, Inc.

    Aliasing

    "import namespace or class, but refer to it

    using this name" PHP utilizes the as keyword to alias

  • 8/3/2019 2011-03-02-Namespaces-v2

    61/84

    All rights reserved. Zend Technologies, Inc.

    Aliasing

    namespace Your;use My as m;$r = new m\Registry();

    namespace Your;use My\Registry as reg;$r = new reg();

    namespace Your;

    useMyas m;$r=new m\Registry();

    namespace Your;use My\Registryas reg;$r=new reg();

  • 8/3/2019 2011-03-02-Namespaces-v2

    62/84

    All rights reserved. Zend Technologies, Inc.

    Make typehinting more semantic!

    namespace App;

    use Zend\EventManager\EventManager as Events;

    class Foo{ public function events(Events $events = null)

    { if ($eventsinstanceof Events) { $this->events = $events;

    } elseif (!$this->events instanceof Events) { $this->events = new Events(__CLASS__);

    } return$this->events;

    }}

    namespace App;

    useZend\EventManager\EventManagerasEvents;

    classFoo{ public functionevents(Events$events=null) { if($eventsinstanceofEvents) { $this->events=$events; }elseif(!$this->eventsinstanceofEvents) { $this->events=newEvents(__CLASS__); } return$this->events; }}

  • 8/3/2019 2011-03-02-Namespaces-v2

    63/84

    All rights reserved. Zend Technologies, Inc.

    Importing multiple namespaces/classes

    Multiple use statements

    namespace Their;use My;use Your;

    namespace Their;use My;use Your;

  • 8/3/2019 2011-03-02-Namespaces-v2

    64/84

    All rights reserved. Zend Technologies, Inc.

    Importing multiple namespaces/classes

    Or use a comma (,) to separate statements

    namespace Their;use My,

    Your;

    namespace Their;use My,

    Your;

  • 8/3/2019 2011-03-02-Namespaces-v2

    65/84

    All rights reserved. Zend Technologies, Inc.

    Importing multiple namespaces/classes

    With aliases

    namespace Their;

    use My as m, // aliasedYour; // not aliased

    namespace Their;

    useMyas m,// aliasedYour; // not aliased

  • 8/3/2019 2011-03-02-Namespaces-v2

    66/84

    All rights reserved. Zend Technologies, Inc.

    What namespace are you in?

    __NAMESPACE__

  • 8/3/2019 2011-03-02-Namespaces-v2

    67/84

    All rights reserved. Zend Technologies, Inc.

    Comprehensive example

    namespace My\Package;const APIKEY = 1;function get($data) {}class Service {}

    namespace My\OtherPackage;const APIKEY = 2;function get($data) {}class Service {}

    namespace My\Package;constAPIKEY=1;function get($data) {}classService{}

    namespace My\OtherPackage;constAPIKEY=2;function get($data) {}classService{}

    The setup:

  • 8/3/2019 2011-03-02-Namespaces-v2

    68/84

    All rights reserved. Zend Technologies, Inc.

    Comprehensive example

    namespace Test;use My\Package\Service as PackageService,

    My\OtherPackage;const APIKEY = 3;echo APIKEY; // 3echo OtherPackage\APIKEY; // 2echo \My\Package\APIKEY; // 1

    $s = new PackageService(); // My\Package\Service$s = new Service(); // E_FATAL (no match)$s = new OtherPackage\Service();

    // My\OtherPackage\Service

    get($baz); // E_FATAL (no matching function in// current namespace)

    OtherPackage\get($baz); // My\OtherPackage\get()

    namespace Test;use My\Package\Serviceas PackageService,

    My\OtherPackage;constAPIKEY=3;echo APIKEY; // 3echo OtherPackage\APIKEY;// 2echo\My\Package\APIKEY; // 1

    $s=new PackageService();// My\Package\Service$s=new Service(); // E_FATAL (no match)$s=new OtherPackage\Service();

    // My\OtherPackage\Service

    get($baz

    ); // E_FATAL (no matching function in // current namespace)OtherPackage\get($baz);// My\OtherPackage\get()

  • 8/3/2019 2011-03-02-Namespaces-v2

    69/84

  • 8/3/2019 2011-03-02-Namespaces-v2

    70/84

    All rights reserved. Zend Technologies, Inc.

    Referencing namespaced code in strings

    Classes, constants, and functions referenced in

    a string MUSTbe fully qualified No namespace separatorprefixis necessary

  • 8/3/2019 2011-03-02-Namespaces-v2

    71/84

    All rights reserved. Zend Technologies, Inc.

    Referencing namespaced code in strings

    Example 1

    namespace My;// Assume My\Log\Logger is a defined class

    $class = 'Log\Logger';$o = new$class; // E_FATAL; can't find

    // relative names

    $class = 'My\Log\Logger';$o = new$class; // Success

    $class = '\My\Log\Logger';$o = new$class; // Also success,

    // but not necessary

    namespace My;// Assume My\Log\Logger is a defined class

    $class='Log\Logger';$o =new$class;// E_FATAL; can't find

    // relative names

    $class='My\Log\Logger';$o =new$class;// Success

    $class='\My\Log\Logger';

    $o =new$class;// Also success,// but not necessary

  • 8/3/2019 2011-03-02-Namespaces-v2

    72/84

    All rights reserved. Zend Technologies, Inc.

    Referencing namespaced code in strings

    Example 2

    namespace My;// Assume My\Log\Logger is a defined class

    $class = 'Log\Logger';if ($oinstanceof$class) { } // Fails; can't

    // resolve class

    $class = 'My\Log\Logger';if ($oinstanceof$class) { } // Success

    namespace My;// Assume My\Log\Logger is a defined class

    $class='Log\Logger';if($oinstanceof$class) { }// Fails; can't

    // resolve class

    $class='My\Log\Logger';if($oinstanceof$class) { }// Success

  • 8/3/2019 2011-03-02-Namespaces-v2

    73/84

  • 8/3/2019 2011-03-02-Namespaces-v2

    74/84

    All rights reserved. Zend Technologies, Inc.

    Code Organization: Filesystem

    Namespace separator has an affinity for the

    directory separator Suggests a 1:1 relationship with file system

    namespace My\Log;class Logger {}/* My/Log/Logger.php (*nix)

    My\Log\Logger.php (Windows) */

    namespace My\Log;classLogger{}/* My/Log/Logger.php (*nix)

    My\Log\Logger.php (Windows) */

  • 8/3/2019 2011-03-02-Namespaces-v2

    75/84

    All rights reserved. Zend Technologies, Inc.

    Code Organization: By Responsibility

    Interfaces

    Use cases: i nst an c eof: $ c l a s s i n s t a n c e o f A d a p t er

    i m pl e m ent s: S o m e C l a s s i m p l e m e n t s A d a p t e r

    Natural language is easiest to understand

    Natural language suggests a hierarchy

  • 8/3/2019 2011-03-02-Namespaces-v2

    76/84

  • 8/3/2019 2011-03-02-Namespaces-v2

    77/84

    All rights reserved. Zend Technologies, Inc.

    Code Organization: By Responsibility

    Takeaway: we're referencing the capabilities,

    not the language type

  • 8/3/2019 2011-03-02-Namespaces-v2

    78/84

    All rights reserved. Zend Technologies, Inc.

    Readability

    When within a namespace, reference classes

    directly, keeping usage succinctnamespace Zend\Http;$client = new Client();

    namespace Zend\Http;$client=newClient();

  • 8/3/2019 2011-03-02-Namespaces-v2

    79/84

    All rights reserved. Zend Technologies, Inc.

    Readability

    Importing and aliasing make names more

    succinctnamespace Application;use Zend\Http\Client as HttpClient,

    Zend\Logger\Logger;

    $client = new HttpClient();$log = new Logger();

    namespace Application;useZend\Http\ClientasHttpClient,

    Zend\Logger\Logger;

    $client=newHttpClient();$log =newLogger();

  • 8/3/2019 2011-03-02-Namespaces-v2

    80/84

    All rights reserved. Zend Technologies, Inc.

    Readability

    Aliasing allows giving names context

    namespace Application\Controller;

    use Zend\Controller\Actionas ActionController;

    class FooControllerextends ActionController {}

    namespace Application\Controller;

    useZend\Controller\ActionasActionController;

    classFooControllerextendsActionController{}

  • 8/3/2019 2011-03-02-Namespaces-v2

    81/84

    All rights reserved. Zend Technologies, Inc.

    Dependency declarations

    Suggestion: import everyclass outside the

    current namespace that you consume

    namespace Application\Controller;

    use Zend\Controller\Actionas ActionController,

    My\ORM\Mapper as DataMapper,My\Entity\BlogEntry,Zend\EventManager\EventManager;

    namespace Application\Controller;

    useZend\Controller\ActionasActionController,

    My\ORM\MapperasDataMapper,My\Entity\BlogEntry,Zend\EventManager\EventManager;

  • 8/3/2019 2011-03-02-Namespaces-v2

    82/84

    All rights reserved. Zend Technologies, Inc.

    Dependency declarations

    Imports are hints to the interpreter, and don't

    cost anything Helps to document dependencies up front

    Allows static analysis to determinedependencies

  • 8/3/2019 2011-03-02-Namespaces-v2

    83/84

  • 8/3/2019 2011-03-02-Namespaces-v2

    84/84

    All rights reserved. Zend Technologies, Inc.

    Click to add title

    PHP Manual: http://php.net/namespace