如何获得父属性当我创建一个新的我的孩子。请注意,我的类不需要继承,因为我的目的只是为了显示一对多关系。由于
class Parent{
$property;
//getter and setter
}
class Child{
$parent; //for Parent Type
}
myChild = new myChild();
echo myChild->parent->property;// this line does not work for me
您可以使用get_parent_class()
。如以下:
<?php
class parent {
function parent_class()
{
// implements some logic
}
}
class child extends parent {
function __construct()
{
echo get_parent_class($this);
}
}
$CHILD = new child();
?>
使用parent::
class A {
function example() {
echo "I'm parent";
}
}
class B extends A {
function example() {
echo "I'm child";
parent::example();
}
}
$b = new B;
// This will call B::example(), which will in turn call A::example().
$b->example();