Usage of class-level constants in php5.5

Source: Internet
Author: User

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.

Copy codeThe Code is as follows:
<? 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:

Copy codeThe Code is as follows:
<? 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:

Copy codeThe Code is as follows:
<? Php
Use vendorpackageFoo;
Class FooTest extends PHPUnit_Framework_TestCase
{
Public function testBarCanBeProcessed ()
{
$ Bar = $ this-> getMock ('vendorpackagebar ');
$ Foo = new Foo;
$ Foo-> process ($ bar );
//...
}
}

Copy codeThe Code is as follows:
<? 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 );
//...
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.