Instructions for using class-level constants in php5.5_PHP Tutorial
WBOYOriginal
2016-07-13 17:16:421031browse
I studied php5.5 yesterday and found that there is a new feature called class-level constants. Let me briefly share my study notes with you.
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.
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:
The code is as follows
Copy code
代码如下
复制代码
use vendorpackageFoo;
class FooTest extends PHPUnit_Framework_TestCase
{
public function testBarCanBeProcessed()
{
$bar = $this->getMock('vendorpackageBar');
$foo = new Foo;
$foo->process($bar);
// ...
}
}
use vendorpackageFoo;
use vendorpackageBar;
class FooTest extends PHPUnit_Framework_TestCase
{
public function testBarCanBeProcessed()
{
$bar = $this->getMock(Bar::CLASS);
$foo = new Foo;
$foo->process($bar);
// ...
}
}
However, when you need to fully qualify the name, the namespace references the class namespace alias... then it gets interesting
.
In the example below:
The code is as follows
Copy code
use vendorpackageFoo;
class FooTest extends PHPUnit_Framework_TestCase
{
Public function testBarCanBeProcessed()
{
$bar = $this->getMock('vendorpackageBar');
$foo = new Foo;
$foo->process($bar);
//
}
}
http://www.bkjia.com/PHPjc/628622.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/628622.htmlTechArticleI studied php5.5 yesterday and found that there is a new feature which is class-level constants. Let me tell you about it below Let me briefly share my study notes. Not long ago, php just released the first stable version of 5.5...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn