Not long ago, php just released the first stable version of 5.5, introducing a CLASS-level constant named 'class' which is valid for all classes and returns the full name of the CLASS.
Copy codeThe Code is as follows:
<? Php
Namespace vendorpackage;
Class Foo
{
//...
}
Var_dump (Foo: CLASS );
// The above script outputs string (18) "vendorpackageFoo ".
Why use it?
Why do we need to use such a constant? Of course, we don't just get the full name of the class as in the above example. We can use _ NAMESPACE _ to achieve the same effect, and php5.3 can be used:
Copy codeThe Code is as follows:
<? Php
Namespace vendorpackage;
Class Foo
{
//...
}
Var_dump (_ NAMESPACE _. 'foo ');
However, when you need a fully qualified name, The namespace references the class namespace alias... Then it becomes interesting.
In the following example:
Copy codeThe Code is as follows:
<? Php
Use vendorpackageFoo;
Class FooTest extends PHPUnit_Framework_TestCase
{
Public function testBarCanBeProcessed ()
{
$ Bar = $ this-> getMock ('vendorpackagebar ');
$ Foo = new Foo;
$ Foo-> process ($ bar );
//...
}
}
Copy codeThe Code is as follows:
<? Php
Use vendorpackageFoo;
Use vendorpackageBar;
Class FooTest extends PHPUnit_Framework_TestCase
{
Public function testBarCanBeProcessed ()
{
$ Bar = $ this-> getMock (Bar: CLASS );
$ Foo = new Foo;
$ Foo-> process ($ bar );
//...
}
}