I have a file with a class and a function definition, according to the PSR-0 definitions (with autoloading):
namespace Foo;
function b() {};
class Bar {}
And I have the test for that class, place in the same namespace:
namespace Foo;
class BarTest {}
When I try to access the b()
function inside the test class, I get a undefined function
error:
namespace Foo;
class BarTest extends PHPUnit_Framework_TestCase
{
public function testSomething()
{
b();
Foo\b();
\b();
}
}
Nothing seems to work. How can I call that function?
PHP autoloading does not support functions. However it does work for static class methods:
namespace Foo;
abstract class Util
{
static function doSomething() {
}
}
You can then use
that class in some other files current namespace and call the static method:
use Foo\Util;
Util::doSomething();
As this is a class method and autoloading is for classes, this does work then.
Take care that the more correct way to group methods inside a namespace / class is probably making it final
Docs as well, however PHP does not support that (see Cannot create a final abstract classPHP Sadness #41).
You find this as well outlined in the following Q&A Material here on the site: