简体   繁体   English

您如何访问子 class 中的父 object 属性

[英]how do you access the parent object property in child class

Actually,实际上,

This is working fine.. but i want the parent value.这工作正常..但我想要父值。 not a child value..不是子值..

<?php
    class Fruit{
        protected $parentproperty = "parentvalue";// this is parent value       
    }

    class Orange extends Fruit{
        protected $parentproperty = "child value";// here i mentioned once again        
        function showParentProperty(){
            return self::$this->parentproperty;
        }


    }
    $obj = new Orange;
    echo $obj->showParentProperty();

    //conclusion:
    // i want to get the parent value not child. its working fine . but it's displaying chid value


?>

If what you mean is this:如果你的意思是这样的:

class Fruit {
    protected $parentproperty = "parent value";
}

class Orange extends Fruit{
    protected $parentproperty = "child value";
    function showParentProperty(){
        return $this->parentproperty; // corrected syntax here
    }
}

Then there is no way to do what you want because all nonstatic class properties in PHP are effectively virtual.那么就没有办法做你想做的事,因为 PHP 中的所有非静态 class 属性实际上都是虚拟的。

You could only use the parent keyword if the parent property was static, like this:如果父属性为 static,则只能使用parent关键字,如下所示:

class Fruit {
    static $parentproperty = "parent value";
}

If we 're talking about an instance property, then the only thing you can do is use another name for the child property.如果我们谈论的是实例属性,那么您唯一能做的就是为子属性使用另一个名称。

It is possible to access default value of non-static property of the parent class which was overridden in the child class:可以访问在子 class 中被覆盖的父 class 的非静态属性的默认值:

class Orange extends Fruit
{
    protected $parentproperty = "child value";
    function showParentProperty()
    {
        $parentProps = get_class_vars(get_parent_class($this));
        return $parentProps['parentproperty'];
    }
}

This way you can access protected and public non-static properties of the parent class which is really handy when applying the DRY principle.通过这种方式,您可以访问父 class 的protectedpublic非静态属性,这在应用 DRY 原则时非常方便。

As you override the class attribute $parentproperty in the child class, I suppose that the value from the parent class is lost.当您覆盖子 class 中的 class 属性 $parentproperty 时,我想来自父 class 的值丢失了。

I think it will work this way.我认为它会以这种方式工作。 What you can do is declare properties in the parent, and then access them directly in the child as long as the child extends the parent.您可以做的是在父级中声明属性,然后只要子级扩展父级,就可以直接在子级中访问它们。 Then, in the child __constructor call the parent __constructor...然后,在子 __constructor 中调用父 __constructor...

class fruit{
   protected $name;
   protected $type;

   __constructor($name, $type){
     $this->name = $name;
     $this->type = $type;
   }//end __constructor

}//end class

class thisFruit extends fruit{
   __constructor($name, $type){
      parent::__constructor($name, $type);
   }
}

You would use it $myFruit = new thisFruit("Orange", "Citrus");你会使用它 $myFruit = new thisFruit("Orange", "Citrus"); echo "This fruit is an ".$myFruit->name; echo "这个水果是一个 ".$myFruit->name;

Hope this helps - good luck!希望这有帮助,祝你好运!

maybe try to use __get($value) in parent class and call it from child by也许尝试在父 class 中使用 __get($value) 并通过子调用它
public function __get($value){ if($value == 'parentValue'){ return parent::__get($value); }else{ return $this->$value; } }
I used it in my current project and works fine.我在我当前的项目中使用它并且工作正常。
Kori科里

In your child:在您的孩子中:

function showParentProperty()
{
    return parent::$parentproperty;
}

the only way to do what you ask without a static property is to use reflexion api没有 static 属性的唯一方法是使用反射 api

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM