访问类的父类的恒定出侧的值I有两个类
类A作为父
乙级作为子
A类和B类都具有相同的恒定的一些值
我初始化子类b
现在,如果我想访问使用子类对象的类的父类常量的值$ test
我该怎么做。
对于如$test::version
//输出2.5.0,我想要什么2.4.0如何使用子类
<?php
class a{
const version='2.4.0';
public function childClassVersion(){
echo static::version;
}
public function parentClassVersion(){
echo self::version;
}
}
class b extends a{
const version='2.5.0';
}
$test=new b;
echo $test::version; // output 2.5.0
echo "<br>";
echo b::version; // output 2.5.0
echo "<br>";
echo a::version; //output 2.4.0
echo "<br>";
$test->childClassVersion(); // output 2.5.0
echo "<br>";
$test->parentClassVersion(); // output 2.4.0
echo "<br>";
对不起,目前尚不清楚你的问题是什么。您可以在自己展示时访问这两个值,那么缺少了什么? – arkascha
使用子类对象访问父类常量 – daulat
也许您在寻找'parent :: version'? – arkascha