Introduction to the use of class-level constants in php5.5_PHP tutorial

WBOY
Release: 2016-07-21 16:13:13
Original
904 people have browsed it

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

TechArticleNot 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...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template