Instructions for using class-level constants in php5.5_PHP Tutorial

WBOY
Release: 2016-07-13 17:16:42
Original
1030 people have browsed it

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.

The code is as follows
 代码如下 复制代码

namespace vendorpackage;
class Foo
{
// ...
}
var_dump(Foo::CLASS);
//上面脚本输出 string(18) "vendorpackageFoo".

Copy code

namespace vendorpackage;
class Foo
{
// ...
}
var_dump(Foo::CLASS);
//The above script outputs string(18) "vendorpackageFoo".

代码如下 复制代码

namespace vendorpackage;
class Foo
{
// ...
}
var_dump(__NAMESPACE__ . 'Foo');

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:

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);
        // ...
    }
}

namespace vendorpackage;
class Foo
{
// ...
}
var_dump(__NAMESPACE__ . 'Foo');

代码如下 复制代码
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

.

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 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