Home php教程 php手册 php5.5中类级别的常量使用介绍_php基础

php5.5中类级别的常量使用介绍_php基础

May 17, 2016 am 08:59 AM
constant

不久前php刚发布了5.5的第一个稳定版,介绍了一个类级别的常量,名字是 `CLASS` 这个常量对所有的类有效,返回类的全名。

复制代码 代码如下:

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

为什么要使用它

我们为什么要使用一个这样的常量,当然不是像上面那个例子一样只是获得类的全名。我们使用__NAMESPACE__也可以达到同样的效果,而且php5.3就可以用了:

复制代码 代码如下:

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

然而,当你需要完全限定名称,命名空间引用了类命名空间别名…然后它变得有趣。

在下面的例子:

复制代码 代码如下:

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

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are constants in C language? Can you give an example? What are constants in C language? Can you give an example? Aug 28, 2023 pm 10:45 PM

What are constants in C language? Can you give an example?

How to create a constant in Python? How to create a constant in Python? Aug 29, 2023 pm 05:17 PM

How to create a constant in Python?

In Java, is it possible to define a constant using only the final keyword? In Java, is it possible to define a constant using only the final keyword? Sep 20, 2023 pm 04:17 PM

In Java, is it possible to define a constant using only the final keyword?

PHP error: How to solve the problem when calling undefined constant? PHP error: How to solve the problem when calling undefined constant? Aug 26, 2023 pm 03:39 PM

PHP error: How to solve the problem when calling undefined constant?

PHP error: What should I do if I use an undefined constant as a property name? PHP error: What should I do if I use an undefined constant as a property name? Aug 17, 2023 pm 02:13 PM

PHP error: What should I do if I use an undefined constant as a property name?

Definition and initialization methods of basic data type constants study guide Definition and initialization methods of basic data type constants study guide Jan 05, 2024 pm 02:08 PM

Definition and initialization methods of basic data type constants study guide

FILTER_SANITIZE_SPECIAL_CHARS constant in PHP FILTER_SANITIZE_SPECIAL_CHARS constant in PHP Aug 20, 2023 pm 09:58 PM

FILTER_SANITIZE_SPECIAL_CHARS constant in PHP

Naming conventions in PHP: How to name constants and file names using underscore nomenclature Naming conventions in PHP: How to name constants and file names using underscore nomenclature Jul 30, 2023 am 10:36 AM

Naming conventions in PHP: How to name constants and file names using underscore nomenclature

See all articles