Today on php-internals David Zülke posted an interesting example with namespaces which made me think about the result of the small example. Well, my first guess was not right as my experiments showed. Therefore I tried different versions of the example to see the impact of namespace and use declarations. But first, the example:
Version 2, moved the inclusion of somestuff.php:
require_once('setup.php');
require_once('somestuff.php');
use Name::Space;
$d = new DateTime();
var_dump($d);
someStuffWeDoNotHaveControlOver();
$d = new DateTime();
var_dump($d);
Same result as version 1.
Version 3, use Name::Space means use Name::Space as Space:
require_once('setup.php');
require_once('somestuff.php');
use Name::Space;
$d = new Space::DateTime();
var_dump($d);
someStuffWeDoNotHaveControlOver();
$d = new Space::DateTime();
var_dump($d);
Result:
loading Name::Space::DateTime
object(Name::Space::DateTime)#1 (0) {
}
object(Name::Space::DateTime)#2 (0) {
}
object(Name::Space::DateTime)#2 (0) {
}
Version 4, lets put our example into its own namespace:
namespace foo;
require_once('setup.php');
require_once('somestuff.php');
use Name::Space;
$d = new DateTime();
var_dump($d);
// we need to prepend the function name with ::
// else PHP will try to call foo::someStuffWeDoNotHaveControlOver() which does not exist
::someStuffWeDoNotHaveControlOver();
$d = new DateTime();
var_dump($d);
Result:
object(DateTime)#1 (0) {
}
loading Name::Space::DateTime
object(Name::Space::DateTime)#2 (0) {
}
object(DateTime)#2 (0) {
}
Version 5, version 3 with example in its own namespace:
namespace foo;
require_once('setup.php');
require_once('somestuff.php');
use Name::Space;
$d = new Space::DateTime();
var_dump($d);
// we need to prepend the function name with ::
// else PHP will try to call foo::someStuffWeDoNotHaveControlOver() which does not exist
::someStuffWeDoNotHaveControlOver();
$d = new Space::DateTime();
var_dump($d);
Result:
loading Name::Space::DateTime
object(Name::Space::DateTime)#1 (0) {
}
object(Name::Space::DateTime)#2 (0) {
}
object(Name::Space::DateTime)#2 (0) {
}
Version 6, create the shortcut for the class instead for the namespace only:
namespace foo;
require_once('setup.php');
require_once('somestuff.php');
use Name::Space::DateTime;
$d = new DateTime();
var_dump($d);
// we need to prepend the function name with ::
// else PHP will try to call foo::someStuffWeDoNotHaveControlOver() which does not exist
::someStuffWeDoNotHaveControlOver();
$d = new DateTime();
var_dump($d);
Same result as version 5, is equivalent.
If you want to create an instance of PHP's DateTime class here you would need to do new ::DateTime:
namespace foo;
require_once('setup.php');
require_once('somestuff.php');
use Name::Space::DateTime;
$d = new ::DateTime();
var_dump($d);
// we need to prepend the function name with ::
// else PHP will try to call foo::someStuffWeDoNotHaveControlOver() which does not exist
::someStuffWeDoNotHaveControlOver();
$d = new ::DateTime();
var_dump($d);
Result:
object(DateTime)#1 (0) {
}
loading Name::Space::DateTime
object(Name::Space::DateTime)#2 (0) {
}
object(DateTime)#2 (0) {
}
Consequences? You will need strict rules for the usage of "use" in your project. Mixing different styles will result in difficulties to see what the code does. Additionally it seems to be a good idea to always do "::ClassName" when accessing global classes as it makes a clear statement about the source of the class.
So I put down this namespace examples together last night. But, what's happening in the examples regarding to resolution rules? (I won't post the code here again, have a look at the last entry for the example code.) Version 1 and 2 two are the same, th
Tracked: Dec 12, 10:16