如何在 PHP 5.2 中的子类中创建一个常量,以便在父类中找到的方法中使用?
编辑:
*注意:不幸的是,我暂时使用 PHP 5.2。我找不到提供 5.3 的像样便宜主机...
在 PHP 中,self
指的是定义被调用方法的类。这意味着,如果您不重写子类中的方法,则关键字 self
将引用父类,即使从子类调用也是如此。
例如,此代码:
<?php
class ParentClass {
const NAME = "ParentClass";
public function showName() {
echo self::NAME . "<br />\n";
}
}
class ChildClass extends ParentClass {
const NAME = "ChildClass";
public function __construct() {
echo self::NAME . "<br />\n";
}
}
$test = new ChildClass();
$test->showName();
?>
将创建此输出:
ChildClass
ParentClass
我想要做的是创建一个默认方法(例如上例中的 showName()
),该方法存在于父类中,并具有定义默认值的常量使用。在子级中,我希望能够重写这些常量(请注意上面子级定义中的 const),并在我在子级实例上调用方法时使用这些值。
简而言之,我怎样才能使上述示例的输出...
ChildClass
ChildClass
...而不必在子级中复制父级的代码?
Edit:
*Note: I'm using PHP 5.2 for the time being, unfortunately. I can't find a decent cheap host offering 5.3...
In PHP, self
refers to the class in which the called method is defined. This means that if you don't override a method in the child class, the keyword self
will refer to the parent class, even when called from the child.
For example, this code:
<?php
class ParentClass {
const NAME = "ParentClass";
public function showName() {
echo self::NAME . "<br />\n";
}
}
class ChildClass extends ParentClass {
const NAME = "ChildClass";
public function __construct() {
echo self::NAME . "<br />\n";
}
}
$test = new ChildClass();
$test->showName();
?>
Will create this output:
ChildClass
ParentClass
What I want to do is to create a default method (e.g. showName()
in the example above) which exists in a parent class with constants defining default values to use. In the child, I want to be able to override these constants (note the const
in the child definition above), and have those values be used when I call the method on an instance of the child.
In short, how can I make it so that the output of the above sample would be...
ChildClass
ChildClass
...without having to duplicate the code of the parent within the child?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试
使用后期静态绑定:
编辑:对于 5.2.x
如果您没有 5.3.0,您将无法利用此功能。一种常见的
hack解决方案是创建一个由子类名引用的静态缓存(例如private static $statics = array()
)。它要求您跟踪对象继承以覆盖 __construct 上的值,并显式定义哪些静态是“可继承的”。例如,SilverStripe 在 Sapphire ORM 中使用此技术来绕过 PHP 静态绑定限制。他们定义了一个基本对象类,以及各种静态变量管理功能。Try
This uses late static binding:
EDIT: For 5.2.x
If you don't have 5.3.0 you won't be able to take advantage of this. One common
hacksolution is to create a static cache (e.g.private static $statics = array()
) referenced by child class name. It requires you to track object inheritance to override the value on __construct, and to explicitly define which statics are 'inheritable'. For example, SilverStripe uses this technique in the Sapphire ORM to get around PHP static binding limitations. They define a base Object class, and various static var management functions.我相信您的情况的相应语法盐是:
I believe the according syntactic salt for your case is:
也许这只是因为你的例子很短,但“自我”引用似乎并不需要。
我只想这样做:
输出:
Maybe it's just because of your short example but the "self" referencing doesn't seem needed.
I would just do this:
Output:
使用
static::CONSTNAME
将返回正确的值。Using
static::CONSTNAME
will return the right value.