开发者

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{
      开发者_如何学Go  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.

You could only use the parent keyword if the parent property was static, like this:

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.


As you override the class attribute $parentproperty in the child class, I suppose that the value from the parent class is lost.


It is possible to access default value of non-static property of the parent class which was overridden in the child 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.


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...

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"); echo "This fruit is an ".$myFruit->name;

Hope this helps - good luck!


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


maybe try to use __get($value) in parent class and call it from child by
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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜