I have a line of PHP that calls a new class as such:
$habits = new \Order\VO\Habits();
To me this looks like a static method call, but I can't tell what exactly is going on. I know that I have a class that follows that name space, and it is just a class that contains a bunch of variables. I can tell that this class is being instantiated, but I can't understand what the purpose for doing it in this manner is. I've never seen a class in PHP called this way, so any documentation as to what this actually is and why the class is being called in this way would be very helpful.
The project I'm in uses the Zend-Framework, so I'm not sure if this is something unique to the framework itself?
EDIT:
The way the class is being called is not a problem, I just want to understand more about what calling a class that was does, and WHY it would be better to call this specific class this way as opposed to simply using
$habits = new habits();
A simple complete example may help. Here I'm using the braced namespace syntax to scope the code into three namespaces in the one file, \Order\VO
, \Lifehacks
, and the global namespace.
<?php
namespace Order\VO {
class Habits {
function __construct() {
echo "New VO Habits
";
}
}
}
namespace Lifehacks {
class Habits {
function __construct() {
echo "New Lifehack Habits
";
}
}
}
namespace {
// PHP Fatal error: Class 'Habits' not found
// $foo = new Habits();
// Prints "New VO Habits"
$foo = new \Order\VO\Habits();
// Prints "New Lifehack Habits"
$bar = new \Lifehacks\Habits();
}
Note that when in the global namespace, you can't just instantiate a Habits with new Habits()
, as one doesn't exist in your namespace. You have to choose one or other of the Habits
classes by prefixing them with a namespace.
When you instantiate a class it's calling (at least 99% of the time in PHP 5) the __construct()
function.
class Foo {
public $bar;
public function __construct($arg) {
$this->bar = $arg;
}
}
$class = new Foo('test');
echo $class->bar; // Ouput: test
In a namespace, your class lives inside what is basically a directory. Take Foo
. I can't have two classes named the same thing, but I could put another Foo
under a namespace.
namespace Example;
class Foo {
public static function dump($arg) {
echo $arg;
}
}
So now we need to refer to which Foo
we want. We can do this either by passing the full namespace
$class = new \Foo('test'); // The original Foo is in root namespace
\Example\Foo::dump('test');
Or by using use
use Example\Foo;
Foo::dump('test'); // alias here so we don't need to redeclare the path
Now, PHP doesn't "automagically" load classes. Instead, a common thing to do is to use an autoloader and then placefiles into a directory matching the namespace. So \Example\Foo
would live in example/foo.php
and your autoloader would simply include the file when you called the class.
It is because you are instantiating the class with the namespace, also you can do it this way:
<?php
use Order\VO\Habits;
$habits = new Habits();
Trying for a more concise explanation. $habits = new Habits();
won't work because the class name is Order\VO\Habits
, not Habits
. For convenience, you can import the namespace as in byoigres' answer, which tells PHP that the class Habits
actually means Order\VO\Habits
. After that, $habits = new Habits();
will work.