Transcript

PHP Namespaces and AutoloadingBy Vic Metcalfe

GTA-PHP

Pre-Talk

Slides

Autoloading since 5.0

Namespaces since 5.3

Global Namespace

<?php

class Grid {

//...

}

<?php

class Grid {

//...

}

You organize your files

using folders

Organize your classes using namespaces

With Namespaces

<?php

namespace DataViews;

class Grid {

//...

}

<?php

namespace Charting;

class Grid {

//...

}

Sub-Namespaces<?php

namespace AcmeInc\Charting;

class Grid {

//...

}

<?php

namespace AcmeInc\DataViews;

class Grid {

//...

}

<?php

$dataView = new \AcmeInc\DataViews\Grid;

$chart = new \AcmeInc\Charting\Grid;

Importing Namespaces<?php

namespace Lorem;

class Foo

{

function __construct()

{

echo "I'm a Foo.\n";

}

}

<?php

namespace Ipsum;

class Bar

{

function __construct()

{

echo "I'm a Bar.\n";

}

}

<?php

require_once('foo.php');

require_once('bar.php');

use Lorem\Foo;

use Ipsum\Bar;

new Foo;

new Bar;

Aliasing Namespaces<?php

namespace Lorem;

class Foo

{

function __construct()

{

echo "Lorem Foo.\n";

}

}

<?php

namespace Ipsum;

class Foo

{

function __construct()

{

echo "Ipsum Foo.\n";

}

}

<?php

require_once('lorem.php');

require_once('ipsum.php');

use Lorem\Foo as LorumFoo;

use Ipsum\Foo as IpsumFoo;

new LorumFoo;

new IpsumFoo;

Before

Autoloading:

The Dark AgesDe Molay, last Grand Master of

the Knights-Templars

(Public Domain Image)

app.phpThe list of requires

could get very long!

foo.php bar.php

<?php

class Foo

{

function __construct()

{

echo "I'm a Foo.\n";

}

}

<?php

class Bar

{

function __construct()

{

echo "I'm a Bar.\n";

}

}

<?php

require_once('foo.php');

require_once('bar.php');

$foo = new Foo;

$bar = new Bar;

Used with permission from http://martybpix.deviantart.com/art/Fluttershy-Yay-Opening-308959593

Hurray for

__autoload!

autoloader.php

app.php

<?php

function __autoload($className)

{

$fileName = strtolower($className) . ".php";

require_once($fileName);

}

<?php

require_once('autoloader.php');

$foo = new Foo;

$bar = new Bar;

Remember: The class Foo lives in foo.php, and Bar lives in bar.php.

You can only have one

__autoload() function

Used with permission from http://mcmxvii.deviantart.com/art/lonely-puppy-63391140

spl_autoload_register

Used with permission from https://www.flickr.com/photos/ncfc/2096277438

Class

Names

File

Names

Enter PSR-0

PHP Framework Interop

Group• Called PHP-FIG

• These are the people responsible for PSR’s (PSR =

“PHP Standards Recommendation”)

Which PSR’s prescribe namespace usage?

ALL OF THEM!

PSR-0

Autoloading

PSR-1

Coding Standard

PSR-2

Style Guide

PSR-3

Logger Interface

PSR-4

Autoloading

PSR-0

namespace Vendor\(NameSpace)*;

PSR-0

namespace Vendor\(NameSpace)*;

Namespaces must start

with a vendor name

PSR-0

namespace Vendor\(NameSpace)*;

After the vendor you can

specify as many sub-

namespaces as you wish,

or none at all

PSR-0

Namespaces are converted directly to folder names.

Class names are split by underscores, and then

converted to folders and file names:

class Foo_Bar

Foo\Bar.php

namespace Lorem_Ipsum;

class Foo_Bar

Lorem_Ipsum\Foo\Bar.php

PSR-0project/

src/

AcmeInc/

FooBarPackage/

Services/

Foo.php

Bar.php

tests/

AcmeInc/

FooBarPackage/

Services/

FooTest.php

BarTest.php

Enter PSR-4

PSR-4

Namespace Prefixes

Base Directories

Got rid of the weird _ to folder name thing,

and addressed issues around case sensitivity,

but most importantly…

PSR-4

project/

src/

Foo.php

Bar.php

tests/

FooTest.php

BarTest.php

project/

src/

AcmeInc/

FooBarPackage/

Services/

Foo.php

Bar.php

tests/

AcmeInc/

FooBarPackage/

Services/

FooTest.php

BarTest.php

PSR-4

Base Directory Namespace Prefix

./project/src/ AcmeInc\FooBarPackage\Services

./project/tests/ AcmeInc\FooBarPackage\Services

PSR-4 lets you shorten

your paths.

It does not allow you use

_ to build paths.

Composer

I did a talk on Composer in

October 2013

It was light on slides and heavy on live demo.

Here’s my raw notes:

https://gist.github.com/zymsys/23c0d7824730a2aa2063

Other Notable Autoloaders

Symphony

ClassLoader

spl_autoload

Zend

AutoloaderFactory

Performance

Troubleshooting

Questions?

Thanks for joining us tonight.

See you Friday at TrueNorthPHP!


Recommended