访问 PHP 类常量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5447541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:27:22  来源:igfitidea点击:

Accessing PHP Class Constants

phpoop

提问by pettazz

The PHP manual says

PHP手册说

Like static members, constant values can not be accessed from an instance of the object.

与静态成员一样,不能从对象的实例访问常量值。

which explains why you can't do this

这解释了为什么你不能这样做

$this->inst = new Classname();
echo $this->inst::someconstant;

but then why does this work?

但是为什么这样做呢?

$this->inst = new Classname();
$inst = $this->inst;
echo $inst::someconstant;

回答by Charles

From the PHP interactive shell:

从 PHP 交互式 shell:

php > class Foo { const A = 'a!'; }
php > class Bar { public $foo; }
php > $f = new Foo;
php > $b = new Bar;
php > $b->foo = $f;
php > echo $b->foo::A;
PHP Parse error:  syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM, expecting ',' or ';' in php shell code on line 1

The reason that the former syntax fails is that the PHP parser doesn't know how to resolve the double-colon after the property reference. Whether or not this is intentional is unknown.

前一种语法失败的原因是 PHP 解析器不知道如何解析属性引用后的双冒号。这是否是故意的还不得而知。

The latter syntax works because it's not going through the property directly, but through a local variable, which the parser accepts as something it can work with:

后一种语法有效,因为它不是直接通过属性,而是通过一个局部变量,解析器接受它作为它可以使用的东西:

php > $inst = $b->foo;
php > echo $inst::A;
a!

(Incidentally, this same restriction is in place for anonymous functions as properties. You can't call them directly using parens, you have to first assign them to another variable and then call them from there. This has been fixed in PHP's trunk, but I don't know if they also fixed the double colon syntax.)

(顺便说一句,对于作为属性的匿名函数也有同样的限制。您不能使用括号直接调用它们,您必须先将它们分配给另一个变量,然后从那里调用它们。这已在 PHP 的主干中修复,但是我不知道他们是否也修复了双冒号语法。)

回答by Matthew

To quote the manual:

引用手册:

As of PHP 5.3.0, it's possible to reference the class using a variable. The variable's value can not be a keyword (e.g. self, parent and static).

从 PHP 5.3.0 开始,可以使用变量来引用类。变量的值不能是关键字(例如 self、parent 和 static)。

It goes on to use this example:

继续使用这个例子:

$class = new MyClass();
echo $class::constant."\n"; // As of PHP 5.3.0

So $inst::someconstantis supposed to work.

所以$inst::someconstant应该工作。

As to why $this->inst::someconstantgives a parsing error, I don't know. PHP is funny about some things.

至于为什么$this->inst::someconstant会出现解析错误,我不知道。PHP 在某些方面很有趣。

回答by Andrew

If you are within the class, you can access the constant like this:

如果您在班级中,则可以像这样访问常量:

self::MY_CONSTANT;

For example:

例如:

class MyClass {
    const MY_CONSTANT = 'constant value';

    public function showConstant() {
        echo self::MY_CONSTANT;
    }
}

http://php.net/manual/en/language.oop5.constants.php

http://php.net/manual/en/language.oop5.constants.php

回答by Pranav Rana

php supports accessing class constants from an object instance. Code below is working (checked in phpv5.5.5):

php 支持从对象实例访问类常量。下面的代码正在运行(在 phpv5.5.5 中检查)

<?php
class superheroes{
    const kal_el = 'Superman';
}

$instance = new superheroes;
echo $instance::kal_el;

Source: http://dwellupper.io/post/48/defining-class-constants-in-php

来源:http: //dwellupper.io/post/48/defining-class-constants-in-php