Function and class in the same file: undefined function

I have a file with the definition of the class and function in accordance with the PSR-0 definitions (with autoload):

namespace Foo;

function b() {};

class Bar {}

And I have a test for this class, a place in the same namespace:

namespace Foo;

class BarTest {}

When I try to access a function b()inside a test class, I get an error undefined function:

namespace Foo;

class BarTest extends PHPUnit_Framework_TestCase
{
    public function testSomething()
    {
        b();
        Foo\b();
        \b();
    }
}

Nothing seems to work. What can I call this feature?

+5
source share
2 answers

PHP startup does not support functions. However, it works for static class methods:

namespace Foo;

abstract class Util
{
    static function doSomething() {

    }
}

You can useuse this class in some other existing file namespaces and call the static method:

use Foo\Util;

Util::doSomething();

, , .

, /, , final Docs, PHP (. & shy; PHP Sadness # 41).

, Q & A , :

+1

b(); ,

function b(){};

0

All Articles