Class-level constants in php5.5. 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. I learned php5.5 yesterday and found a new function, which 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: |
|
|
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: |
|
|
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: |
|
|
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: |
|
Use vendorpackageFoo;
Use vendorpackageBar;
Class FooTest extends PHPUnit_Framework_TestCase
{
Public function testBarCanBeProcessed ()
{
$ Bar = $ this-> getMock (Bar: CLASS );
$ Foo = new Foo;
$ Foo-> process ($ bar );
//...
}
} |
Bytes. Not long ago, php just released the first stable version of 5.5...