Page 1 of 1

PHP 5.3 Namespacing and Autoloading Demonstrates how to use namespaces in PHP 5.3 Rate Topic: -----

#1 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Post icon  Posted 08 January 2010 - 06:46 PM

PHP Namespacing and AutoLoading

This tutorial is meant to shed some light over the new namespacing features that arrived with PHP 5.3.

To be able to benefit from this tutorial you should know about object oriented programming and design using PHP and also understand the meaning of autoloading.

---

The most fundamental part to understand about PHP namespacing is how to register and use a namespace and what rules you have to follow in order to use this feature. The most important rule is to always place the namespace first in the PHP file, nothing else can exist before it except for comments and whitespace, and also, all code written in a file that uses namespaces must be put inside a namespace.

You can register a namespace in two different ways, A:
namespace Super\Duper\Namespacing; 

/* Code Comes Here */


or B:
namespace Super\Duper\Namespacing 
{ 
	/* Code Comes Here */
}


I prefer syntax B since it gives the code a more structured look (my opinion).

Here's an example of how you would code a class using namespacing with syntax B:
namespace Super\Duper\Namespacing
{
	class SomeObject
	{
		private $variable;
		
		public function __construct($variable = null)
		{
			$this->setVariable($variable);
		}
	
		public function getVariable() { return $this->variable; }
		public function setVariable($value) { $this->variable = $value; }
	}
}


Let's look at how you could use this namespace in another file. To do this you have to take advantage of the use keyword, example:
use Super\Duper\Namespacing;


Now you could instantiate a SomeObject instance like this:
$someObject = new Namespacing\SomeObject();


To make the code even shorter you could use the alias feature, like this:
use Super\Duper\Namespacing as SDN;

$someObject = new SDN\SomeObject();


Or even:
use Super\Duper\Namespacing\SomeObject as SO;

$someObject = new SO();


An example of using namespaces in a namespaced file:
namespace Super\Duper\Namespacing\Files
{
	use Super\Duper\Namespacing as SDN;
	
	class SomeOtherObject extends SDN\SomeObject
	{
		public function DoNothing() { }
	}
}


Now, how do one autoload classes using namespaces? Well, it is pretty easy, at least if you structure your files in directories named and nestled according to your namespaces; Say we have the namespace "Super\Duper\Namespacing" that has a class called SomeObject; if we structure our files in a way that the SomeObject-class is found in "Super/Duper/Namespacing/SomeObject.php" the only thing we actually have to do is to register the standard spl-autoloader:
spl_autoload_extensions(".php"); // To make sure spl only includes php-files while autoloading.
spl_autoload_register();


And it should work perfectly, simply by using a great file-structure.

An example of using autoloading for our Super\Duper\Namespacing-namespace and great file structure:

File structure:
/
/index.php
/Super
/Super/Duper
/Super/Duper/Namespacing
/Super/Duper/Namespacing/SomeObject.php


Code ( /index.php ):
spl_autoload_extensions(".php");
spl_autoload_register();

use Super\Duper\Namespacing as SDN;

$someObject = new SDN\SomeObject("Hello World");

echo $someObject->getVariable();


Some drawbacks with namespacing:
If you use built-in classes in PHP like for instance the Array-class you always, in a namespaced file, have to refer to the global namespace, example:
namespace MyNamespace
{
	$array = \Array();
}

You must also refer to the global namespace if you extend a built-in class or implements a built-in interface:
namespace MyNamespace
{
	class MyArray extends \Array { }
}


---

This was a short presentation of the namespacing features in PHP 5.3 and I hope you have enjoyed it! :)

Note: all the examples have been tested using PHP 5.3 and Apache HTTP Server 2.2 on Window Vista Home Premium... I can't however guarantee that you will get the same results on other setups.

Attached File(s)



Is This A Good Question/Topic? 3
  • +

Replies To: PHP 5.3 Namespacing and Autoloading

#2 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Posted 01 May 2010 - 06:19 AM

The last part using and extending/inheriting "\Array" should actually be "\ArrayObject" since the PHP Array is not a class that can be inherited or used as a class in such a manner. Never actually thought this article went through, never got a message stating so or anything so I hoped it wouldn't be approved because of that little mistake which I discovered after hitting the submit-button. Well, Well! :)
Was This Post Helpful? 0
  • +
  • -

#3 Guest_shushi*


Reputation:

Posted 01 June 2010 - 12:58 PM

This is used in the hardcore OOP framwork in the company where i work since php 5.0 ( patched ) with :: instead of \
Saves alot time :P
Was This Post Helpful? 0

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3577
  • View blog
  • Posts: 10,444
  • Joined: 08-June 10

Posted 07 July 2010 - 05:20 AM

@Wimpy: I don’t know how you managed to load the classes without an explicit loading function (maybe you used Windows …). All resources I’ve found (and my PHP also) need a function/class, that translates the namespace into a valid path.

EDIT: the PHP Standard Working Group emitted a final Proposal regarding namespace conventions for autoloaders. this suggests that namespaces do not automaticly autoload a file structure in every system.

This post has been edited by Dormilich: 07 July 2010 - 06:08 AM

Was This Post Helpful? 1
  • +
  • -

#5 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2796
  • View blog
  • Posts: 15,911
  • Joined: 24-September 08

Posted 09 July 2010 - 09:39 AM

So... basically namespaces are like Java's packages?
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3577
  • View blog
  • Posts: 10,444
  • Joined: 08-June 10

Posted 10 July 2010 - 12:34 AM

from what I know about Java packages (and thats not much) Id say yes.
Was This Post Helpful? 0
  • +
  • -

#7 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Posted 28 November 2010 - 07:44 AM

@Dormilich: I did use Windows ( Vista Home Premium ), which I stated in the tutorial. However, would the default behavior really differ between the OS's? Why would Windows PHP users benefit great default behavior when other OS's users would not?

According to the documentation the default spl_autoload()-function will be used if no other function is explicitly provided when calling spl_autoload_register() ( which I proposed you to do in the tutorial ). And ( as I also stated in the tutorial ) if you have your file structure match the structure of your namespaces, the default spl_autoload()-function will be able to auto-load your classes automatically ( at least in Windows Vista Home Premium and Windows 7 Home Premium ). I may not have been that clear as to "what function" would auto-load the classes but at least one did ( in Windows ).

This post has been edited by Wimpy: 28 November 2010 - 07:46 AM

Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3577
  • View blog
  • Posts: 10,444
  • Joined: 08-June 10

Posted 28 November 2010 - 07:48 AM

I think the difference lies in the directory separator. windows uses the backslash, the same separator that is used for the namespacing. all other OS I know use the forward slash, so the namespace is just a string to them unrelated to the file system.

besides that, this will screw up if I upload my local files to a webserver.
Was This Post Helpful? 0
  • +
  • -

#9 mappo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 26-November 14

Posted 26 November 2014 - 12:58 PM

Very usefull, thanks! It worked fine with me but I had to change files and folders names to lower cases otherwhise I got fatal errors reporting. Instead a of
/
/index.php
/Super
/Super/Duper
/Super/Duper/Namespacing
/Super/Duper/Namespacing/SomeObject.php

I changed to
/
/index.php
super
/super/duper
/super/duper/namespacing
/super/duper/namespacing/someobject.php

Was It me doing something wrong or just a bug on the tutorial files?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1