Lord Yggdrasill;11019543 wrote:Designing your namespace system based on folder structure also makes it easier for autoloaders, doesnt it?
Yes; that's what I was going to say.
traq;11019523 wrote:This is where use comes in.
*use as. (sorry!)
Actually, we've all been doing it wrong. I double-checked. Aliasing the namespace is pretty much required.
Observe:
<?php
namespace one{
function hello(){ print "hello"; }
}
namespace two{
use \one;
hello();
# Outputs:
# > Fatal error: Call to undefined function two\hello()
}
Whereas:
<?php
namespace one{
function hello(){ print "hello"; }
}
namespace two{
use \one as o;
o\hello();
# Outputs:
# > hello
}
Or:
<?php
namespace one{
function hello(){ print "hello"; }
}
namespace two{
# Note: no `use` statement
\one\hello();
# Outputs:
# > hello
}
And of Course:
<?php
namespace one{
function mysql_connect( $h,$u,$p ){ print "Stop using the mysql_*() functions!"; }
}
namespace one\another{
mysql_connect( 'host','user','pass' );
# there is no one\another\mysql_connect(),
# so PHP falls back on the root namespace
# (_not_ `\one`, but `\` )
# and the "real" mysql_connect() function.
}
namespaces are less like [font=monospace]include[/font] and more like a filesystem: you want the functions in [font=monospace]\my\php\stuff[/font], not the ones in the root (PHP's default) namespace.