Not long ago, PHP released the first stable version of 5.5, which introduced a class-level constant named `CLASS`. This constant is valid for all classes and returns the full name of the class.
Copy code The code is as follows:
namespace vendorpackage;
class Foo
{
// ...
}
var_dump(Foo::CLASS);
//The above script outputs string(18) "vendorpackageFoo".
Why use it
Why should we use such a constant? Of course it is not just to get the full name of the class like the example above. We can also achieve the same effect using __NAMESPACE__, and php5.3 can be used:
Copy code The code is as follows:
namespace vendorpackage;
class Foo
{
// ...
}
var_dump(__NAMESPACE__ . 'Foo');
However, when you need to fully qualify the name, the namespace references the class namespace alias... then it gets interesting.
In the example below:
Copy code The code is as follows:
use vendorpackageFoo;
class FooTest extends PHPUnit_Framework_TestCase
{
public function testBarCanBeProcessed()
{
$bar = $this->getMock('vendorpackageBar');
$foo = new Foo;
$foo-> process($bar);
// ...
}
}
Copy code The code is as follows:
use vendorpackageFoo;
use vendorpackageBar;
class FooTest extends PHPUnit_Framework_TestCase
{
public function testBarCanBeProcessed()
{
$bar = $this->getMock(Bar::CLASS);
$foo = new Foo;
http://www.bkjia.com/PHPjc/313492.html
www.bkjia.com
true
http: //www.bkjia.com/PHPjc/313492.htmlTechArticleNot long ago, PHP just released the first stable version of 5.5, which introduced a class-level constant named `CLASS` This constant is valid for all classes and returns the full name of the class. Copy code Code...