I learned php5.5 yesterday and found that a new function is a Class-level constant. Next I will share my study notes with you.
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.
| The Code is as follows: |
Copy code |
<? 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:
| The Code is as follows: |
Copy code |
<? 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:
| The Code is as follows: |
Copy code |
<? Php Use vendorpackageFoo; Class FooTest extends PHPUnit_Framework_TestCase { Public function testBarCanBeProcessed () { $ Bar = $ this-> getMock ('vendorpackagebar '); $ Foo = new Foo; $ Foo-> process ($ bar ); //... } }
|
| The Code is as follows: |
Copy code |
<? 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 ); //... } } |