如何从父类函数访问子类中定义的常量? [英] How to access constant defined in child class from parent class functions?
本文介绍了如何从父类函数访问子类中定义的常量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我从php.net看到这个示例:
I saw this example from php.net:
<?php
class MyClass {
const MY_CONST = "yonder";
public function __construct() {
$c = get_class( $this );
echo $c::MY_CONST;
}
}
class ChildClass extends MyClass {
const MY_CONST = "bar";
}
$x = new ChildClass(); // prints 'bar'
$y = new MyClass(); // prints 'yonder'
?>
但是$ c :: MY_CONST只能在5.3.0或更高版本中识别。
But $c::MY_CONST is only recognized in version 5.3.0 or later. The class I'm writing may be distributed a lot.
基本上,我在ChildClass中定义了一个常量,MyClass(父类)中的一个函数需要使用常数。任何想法?
Basically, I have defined a constant in ChildClass and one of the functions in MyClass (father class) needs to use the constant. Any idea?
推荐答案
如何使用 static :: MY_CONST
?
这篇关于如何从父类函数访问子类中定义的常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文