Mastering PHP Namespaces PFCongres11

Embed Size (px)

Citation preview

MASTERING NAMESPACES!

Nick BelhommeSpetember 17, 2011 PFCongres
The Netherlands

Software Architect / Project Lead

Author of the Zend Framework 2.0 Cookbook

International Conference Speaker

Contributor to various Open Source Projects

Freelance PHP Consultant

www.nickbelhomme.com

@NickBelhomme

facebook.com/NickBelhommeFanPage

Freenode irc: NickBelhomme

WHO USES NAMESPACES

Other big programming languages support themIntroduced in PHP 5.3 Two years agoModern Next Gen Frameworks (Symfony 2.0, Zend Framework 2.0 and others)Job market demands itUse it or loose it

WHAT ARE THEY ?

Abstract container created to hold a logical grouping of unique identifiers.Unique identifiers are names. (in practice: classes, functions, constants)Each identifier is associated with the namespace where it is defined.

Namespace Zend\Controller\Action

Namespace Zend\View

Namespace Nbe

HelperLoader

HelperBroker

HelperPriorityStack

E_ERROR

explode

HelperLoader

HelperBroker

PhpRenderer

Namespace

ArrayObject

explode

E_ERROR

ArrayObject

Abstract container created to hold a logical grouping of unique identifiers.Unique identifiers are names. (in practice: classes, functions, constants)Each identifier is associated with the namespace where it is defined.

Namespace Zend\Controller\Action

Namespace Zend\View

Namespace Nbe

HelperLoader

HelperBroker

HelperPriorityStack

E_ERROR

explode

HelperLoader

HelperBroker

PhpRenderer

Namespace

ArrayObject

explode

E_ERROR

ArrayObject

Abstract container created to hold a logical grouping of unique identifiers.Unique identifiers are names. (in practice: classes, functions, constants)Each identifier is associated with the namespace where it is defined.

Namespace Zend\Controller\Action

Namespace Zend\View

Namespace Nbe

HelperLoader

HelperBroker

HelperPriorityStack

E_ERROR

explode

HelperLoader

HelperBroker

PhpRenderer

Namespace

ArrayObject

explode

E_ERROR

ArrayObject

Abstract container created to hold a logical grouping of unique identifiers.Unique identifiers are names. (in practice: classes, functions, constants)Each identifier is associated with the namespace where it is defined.

Namespace Zend\Controller\Action

Namespace Zend\View

Namespace Nbe

HelperLoader

HelperBroker

HelperPriorityStack

E_ERROR

explode

HelperLoader

HelperBroker

PhpRenderer

Namespace

ArrayObject

explode

E_ERROR

ArrayObject

Abstract container created to hold a logical grouping of unique identifiers.Unique identifiers are names. (in practice: classes, functions, constants)Each identifier is associated with the namespace where it is defined.

Namespace Zend\Controller\Action

Namespace Zend\View

Namespace Nbe

HelperLoader

HelperBroker

HelperPriorityStack

E_ERROR

explode

HelperLoader

HelperBroker

PhpRenderer

Namespace

ArrayObject

explode

E_ERROR

ArrayObject

Namespace Separator

\

PEAR Naming Convention R.I.P.

No more clever workaround for lack of namespace support

No more very long function and class names

R.I.P. Poor man's namespacing

Welcome Readability

new Zend_Layout_Controller_Action_Helper_Layout()

class NbeZf_Model_User extends Nbe_Model_Abstract

new Zend_Layout_Controller_Action_Helper_Layout()new Layout()

class NbeZf_Model_User extends Nbe_Model_Abstractclass User extends AbstractModel

100% PURE NAMESPACESNo supplements Pure Power

The container space is created per file at the absolute top

Coming to an end, but first someTimeline changes

PHP4 class constructor has the same name as the class

> PHP5 class constructor is __construct but accepts PHP4 convention

>5.3 namespaces are introduced, nothing changes

>= 5.3.3 Backward compatibility change. Only __construct is now regarded asthe constructor for namespaced code.

And a quick recap on WHY

Readability

Dependencies can be indicated easily

System functions and constants can be overwritten

It is the future, evolve with the language or loose it

The market will soon require the namespace skill

And some cool resources

The PHP manual http://www.php.net/namespaces

Ralph Schindlers namespace convert toolhttps://github.com/ralphschindler/PHPTools

Plus comments on it by Cal Evanshttp://blog.calevans.com/2010/03/27/zends-new-namespace-converter/

Google: php namespaceshttp://www.google.be/search?q=php+namespaces

Zend Framework 2.0 Cookbookhttp://blog.nickbelhomme.com/php/zend-framework-2-0-cookbook_324

And we reached the end,Any questions?

The End!

THANK YOU

[email protected]

Slideshare, Twitter, IRC: NickBelhomme

http://blog.nickbelhomme.com

Photo Credits

Photos rights reserved bySweetie187 ,webtreats,royblumenthal,Brad montgomery,ktylerconk,vegetarians-dominate-meat-eaters-01,endless lazlo,DebilzBG,J. Star,quadrant6ix,notemilytaken from Flickr.com