php5.5 class-level constants usage Instructions _php Tutorial

Source: Internet
Author: User
Learned a bit yesterday php5.5 found a new function is class-level constants, let me give you a simple share of my study notes.

PHP recently released the first stable version of 5.5, introduced a class-level constant, the name is ' class ', the constant is valid for all classes, returns the full name of the class.

The code is as follows Copy Code

namespace Vendorpackage;
Class Foo
{
// ...
}
Var_dump (Foo::class);
The above script outputs a string of "Vendorpackagefoo".

Why do you want to use it

Why do we use a constant like this, not just the full name of the class, as in 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

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 becomes interesting.

In the following example:

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

The code is as follows Copy Code
Use Vendorpackagefoo;
Use Vendorpackagebar;
Class Footest extends Phpunit_framework_testcase
{
Public Function testbarcanbeprocessed ()
{
$bar = $this->getmock (bar::class);
$foo = new Foo;
$foo->process ($bar);
// ...
}
}

http://www.bkjia.com/PHPjc/628622.html www.bkjia.com true http://www.bkjia.com/PHPjc/628622.html techarticle learned a bit yesterday php5.5 found a new function is class-level constants, let me give you a simple share of my study notes. Not long ago PHP just released the first stable of 5.5 ...

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