PHP - 在类中定义常量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5892226/
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
PHP - define constant inside a class
提问by Alex
How can I define a constant inside a class, and make it so it's visible only when called in a class context?
如何在类中定义常量,并使其仅在类上下文中调用时可见?
....something like Foo::app()->MYCONSTANT;
....就像是 Foo::app()->MYCONSTANT;
(and if called like MYCONSTANTto be ignored)
(如果调用喜欢MYCONSTANT被忽略)
回答by Alix Axel
See Class Constants:
请参阅类常量:
class MyClass
{
const MYCONSTANT = 'constant value';
function showConstant() {
echo self::MYCONSTANT. "\n";
}
}
echo MyClass::MYCONSTANT. "\n";
$classname = "MyClass";
echo $classname::MYCONSTANT. "\n"; // As of PHP 5.3.0
$class = new MyClass();
$class->showConstant();
echo $class::MYCONSTANT."\n"; // As of PHP 5.3.0
In this case echoing MYCONSTANTby itself would raise a notice about an undefined constant and output the constantname converted to a string: "MYCONSTANT".
在这种情况下,回显MYCONSTANT本身会引发有关未定义常量的通知,并输出转换为字符串的常量名称:"MYCONSTANT"。
EDIT- Perhaps what you're looking for is this static properties / variables:
编辑- 也许你正在寻找的是这个静态属性/变量:
class MyClass
{
private static $staticVariable = null;
public static function showStaticVariable($value = null)
{
if ((is_null(self::$staticVariable) === true) && (isset($value) === true))
{
self::$staticVariable = $value;
}
return self::$staticVariable;
}
}
MyClass::showStaticVariable(); // null
MyClass::showStaticVariable('constant value'); // "constant value"
MyClass::showStaticVariable('other constant value?'); // "constant value"
MyClass::showStaticVariable(); // "constant value"
回答by matiaslauriti
This is and old question, but now on PHP 7.1 you can define constant visibility.
这是一个老问题,但现在在 PHP 7.1 上,您可以定义恒定可见性。
EXAMPLE
例子
<?php
class Foo {
// As of PHP 7.1.0
public const BAR = 'bar';
private const BAZ = 'baz';
}
echo Foo::BAR . PHP_EOL;
echo Foo::BAZ . PHP_EOL;
?>
Output of the above example in PHP 7.1:
以上示例在 PHP 7.1 中的输出:
bar Fatal error: Uncaught Error: Cannot access private const Foo::BAZ in …
Note:As of PHP 7.1.0 visibility modifiers are allowed for class constants.
注意:自 PHP 7.1.0 起,类常量允许使用可见性修饰符。
More info here
更多信息在这里
回答by deceze
class Foo {
const BAR = 'baz';
}
echo Foo::BAR;
This is the only way to make class constants. These constants are always globally accessible via Foo::BAR, but they're not accessible via just BAR.
这是使类常量的唯一方法。这些常量始终可通过 全局访问Foo::BAR,但不能仅通过BAR.
To achieve a syntax like Foo::baz()->BAR, you would need to return an object from the function baz()of class Foothat has a propertyBAR. That's not a constant though. Any constantyou define is always globally accessible from anywhere and can't be restricted to function call results.
要实现类似的语法Foo::baz()->BAR,您需要从具有属性baz()的类的函数中返回一个对象。但这不是一个常数。您定义的任何常量始终可以从任何地方全局访问,并且不能仅限于函数调用结果。FooBAR
回答by Wouter van Dam
This is a pretty old question, but perhaps this answer can still help someone else.
这是一个很老的问题,但也许这个答案仍然可以帮助其他人。
You can emulate a public constant that is restricted within a class scope by applying the final keyword to a method that returns a pre-defined value, like this:
您可以通过将 final 关键字应用于返回预定义值的方法来模拟限制在类范围内的公共常量,如下所示:
class Foo {
// This is a private constant
final public MYCONSTANT()
{
return 'MYCONSTANT_VALUE';
}
}
The final keyword on a method prevents an extending class from re-defining the method. You can also place the final keyword in front of the class declaration, in which case the keyword prevents class Inheritance.
方法上的 final 关键字可防止扩展类重新定义该方法。您还可以将 final 关键字放在类声明的前面,在这种情况下,关键字会阻止类继承。
To get nearly exactly what Alex was looking for the following code can be used:
可以使用以下代码来获得几乎正是亚历克斯正在寻找的内容:
final class Constants {
public MYCONSTANT()
{
return 'MYCONSTANT_VALUE';
}
}
class Foo {
static public app()
{
return new Constants();
}
}
The emulated constant value would be accessible like this:
可以像这样访问模拟的常量值:
Foo::app()->MYCONSTANT();
回答by Pranav Rana
You can define a class constant in php. But your class constant would be accessible from any object instance as well. This is php's functionality.
However, as of php7.1, you can define your class constants with access modifiers(public, privateor protected).
您可以在 php 中定义一个类常量。但是您的类常量也可以从任何对象实例访问。这是php的功能。但是,从php7.1 开始,您可以使用访问修饰符(public,private或protected)定义类常量。
A work around would be to define your constant as privateor protectedand then make them readable via a static function. This function should only return the constant values if called from the static context.
解决方法是将常量定义为privateor protected,然后通过static function. 如果从静态上下文调用,此函数应仅返回常量值。
You can also create this static function in your parent class and simply inherit this parent class on all other classes to make it a default functionality.
您还可以在父类中创建此静态函数,并在所有其他类上简单地继承此父类,使其成为默认功能。
Credits: http://dwellupper.io/post/48/defining-class-constants-in-php
学分:http: //dwellupper.io/post/48/defining-class-constants-in-php

