Since the version 5.3 we have namespaces in PHP.
Here is how you would define one.
//Pizza.php
namespace Food;
class Pizza{
public function eat(){
return 'yummy!';
}
}
You would call that like this:
//main.php
require 'Pizza.php';
$pizza = new \Food\Pizza();
echo $pizza->eat();
Or like this.
//main.php
require 'Pizza.php';
use Food\Pizza;
$pizza = new Pizza();
echo $pizza->eat();
Note that when you write use
statement you can use both the notations.
use \Food\Pizza;
use Food\Pizza;
But the later one is my favorite.
This is not the case when you write the new
statement
namespace Kings;
$pizza = new Super\Big\Pizza();
// "Class not found Kings\Super\Big\Pizza"
$pizza = new \Super\Big\Pizza();
// This works!
So it is always good to use \ after the new keyword.
Next, go ahead and shorten the long namespaces like this
use Kings\Big\Pizza as KBPizza;
$pizza = new KBPizza();
This will save you some keystrokes.