PHP将父属性值复制到子属性值

PHP将父属性值复制到子属性值,php,inheritance,parent-child,Php,Inheritance,Parent Child,从下面的代码中,有人知道如何将$parent->foo和$parent->foo1的值分别从B中定义的方法放入$child->bar和$child->bar1中吗 我可以用$child->bar=$parent->foo和$child->bar1=$parent->foo1在对象外部复制这些值,但对于这种情况,在子对象(甚至是父对象)中可能会有一个简单的方法。我的父对象从html输入框中获取foo的值 Class A { public $foo="someText"; publ

从下面的代码中,有人知道如何将$parent->foo和$parent->foo1的值分别从B中定义的方法放入$child->bar和$child->bar1中吗

我可以用$child->bar=$parent->foo和$child->bar1=$parent->foo1在对象外部复制这些值,但对于这种情况,在子对象(甚至是父对象)中可能会有一个简单的方法。我的父对象从html输入框中获取foo的值

Class A 
{
    public $foo="someText";
    public $foo1="someOtherText";
}

Class B extends A 
{
    public $bar;
    public $bar1;

    //theoretical function Id like to use
    public copyParentcontents() 
    {

    }
}

$parent = new A();
$child = new B();
然后:

$parent= new A; $child= new B; 
$parent->foo = "modifiedText";
$child->copyParentcontents($parent);

也许这就是您需要的?

在您的示例中,
$child
将从
$parent
继承类参数:

echo $child->foo; // prints "someText";

你是对的,但我相信他正在更改父实例的属性值:“我的父对象正在从html输入框中获取foo的值。”是的,我也是这样理解的,他的示例代码有点误导:)对于误导的代码,我很抱歉!我对OOP还很陌生,试图把所有事情都搞乱$“child”是“$foo”值的最终位置,也是我连接mysql服务器的地方$“家长”是我获取用户输入并进行错误检查和验证的地方。有没有更好和/或更有效的方法来处理我想做的事情?
echo $child->foo; // prints "someText";