PHP 特性 - 定义通用常量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24357985/
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 traits - defining generic constants
提问by Tom Jowitt
What is the best way to define constants that may be used by a number of classes within a namespace? I'm trying to avoid too much inheritance, so extending base classes is not an ideal solution, and I'm struggling to find a good solution using traits. Is this in any way possible in PHP 5.4 or should a different approach be taken?
定义名称空间内多个类可能使用的常量的最佳方法是什么?我试图避免过多的继承,所以扩展基类不是一个理想的解决方案,我正在努力寻找使用特征的好的解决方案。这在 PHP 5.4 中是否可行,还是应该采取不同的方法?
I have the following situation:
我有以下情况:
trait Base
{
// Generic functions
}
class A
{
use Base;
}
class B
{
use Base;
}
The problem is that it is not possible to define constants in PHP traits. Ideally, I would want something like the following:
问题是无法在 PHP 特征中定义常量。理想情况下,我想要如下内容:
trait Base
{
const SOME_CONST = 'someconst';
const SOME_OTHER_CONST = 'someotherconst';
// Generic functions
}
Then these could be accessed though the class that applies the trait:
然后可以通过应用特征的类访问这些:
echo A::SOME_CONST;
echo B::SOME_OTHER_CONST;
But due to the limitations of traits this isn't possible. Any ideas?
但由于特征的限制,这是不可能的。有任何想法吗?
回答by Tom Jowitt
I ended up using user sectus's suggestionof interfaces as it feels like the least-problematic way of handling this. Using an interface to store constants rather than API contracts has a bad smell about it though so maybe this issue is more about OO design than trait implementation.
我最终使用了用户 sectus 的界面建议,因为它感觉是处理这个问题的最不成问题的方法。使用接口来存储常量而不是 API 契约有一个不好的味道,所以也许这个问题更多地是关于 OO 设计而不是 trait 实现。
interface Definition
{
const SOME_CONST = 'someconst';
const SOME_OTHER_CONST = 'someotherconst';
}
trait Base
{
// Generic functions
}
class A implements Definition
{
use Base;
}
class B implements Definition
{
use Base;
}
Which allows for:
这允许:
A::SOME_CONST;
B::SOME_CONST;
回答by Michael
You could also use static variables. They can be used in the class or the trait itself. - Works fine for me as a replacement for const.
您也可以使用静态变量。它们可以在类或特征本身中使用。- 作为 const 的替代品,对我来说效果很好。
trait myTrait {
static $someVarA = "my specific content";
static $someVarB = "my second specific content";
}
class myCustomClass {
use myTrait;
public function hello()
{
return self::$someVarA;
}
}
回答by Ja?ck
To limit the scope of your constants, you can define them inside a namespace:
要限制常量的范围,您可以在命名空间中定义它们:
namespace Test;
const Foo = 123;
// generic functions or classes
echo Foo;
echo namespace\Foo;
A downside of this approach is that autoloading won't work for constants, at least not for 5.4; the typical way around this is to wrap those constants in a static class, i.e.:
这种方法的一个缺点是自动加载不适用于常量,至少不适用于 5.4;解决这个问题的典型方法是将这些常量包装在一个静态类中,即:
namespace Test;
class Bar
{
const Foo = 123;
}
回答by Jekis
Not a good one, but maybe...
不是很好,但也许......
trait Base
{
public static function SOME_CONST()
{
return 'value1';
}
public static function SOME_OTHER_CONST()
{
return 'value2';
}
// generic functions
}
class A
{
use Base;
}
class B
{
use Base;
}
echo A::SOME_CONST();
echo B::SOME_OTHER_CONST();
回答by Osan
Something else to consider is whether or not you can use an abstract class instead, and then inherit.
还需要考虑的是您是否可以使用抽象类代替,然后继承。
abstract class Base
{
const A = 1;
const B = 2;
}
class Class1 extends Base {}
class Class2 extends Base {}
echo Class1::A;
echo Class2::B;
Of course, part of the reason for traits is replace complex inheritance trees with composition. Depends on the situation.
当然,traits 的部分原因是用组合替换复杂的继承树。视情况而定。