简体   繁体   中英

show only parent class properties in child class using $this in parent class

I have the following two classes. Class BMW extends class Car.

class Car{

    public $doors;
    public $wheels;
    public $color;
    public $size;

    public function print_this(){
        print_r($this);
    }

}

class BMW extends Car{
    public $company;
    public $modal;

    public function __construct(){
        print_r(parent::print_this());
    }
}

$bmw = new BMW();
$bmw->print_this();

In above code when I access parent class method from constructor using parent::print_this() and inside print_this() method I have print_r($this) which prints all properties(parent and child class properties) Now what I want print_r(parent::print_this()); should output only parent class properties in child class? Can anyone help me on this?

You can achieve this using reflection :

class Car{
    public $doors;
    public $wheels;
    public $color;
    public $size;

    public function print_this(){
        $class = new ReflectionClass(self::class); //::class works since PHP 5.5+

        // gives only this classe's properties, even when called from a child:
        print_r($class->getProperties());
    }
}

You can even reflect into the parent class from a child class:

class BMW extends Car{
    public $company;
    public $modal;

    public function __construct(){
        $class = new ReflectionClass(self::class);
        $parent = $class->getParentClass();
        print_r($parent->getProperties());
    }
}

Edit:

what actually I want that whenever I access print_this() method using object of class BMW it should print BMW class properties only and when I access print_this() from BMW class using parent it should print only parent class properties.

There are two ways of making the same method behave differently: Overriding it in the child class or overloading it / passing flags to it. Since overriding it would mean a lot of code duplication (you would have to to basically the same in each child class) I would suggest you build the print_this() method on the parent Car class like this:

public function print_this($reflectSelf = false) {
    // make use of the late static binding goodness
    $reflectionClass = $reflectSelf ? self::class : get_called_class();
    $class = new ReflectionClass($reflectionClass);

    // filter only the calling class properties
    $properties = array_filter(
        $class->getProperties(), 
        function($property) use($class) { 
           return $property->getDeclaringClass()->getName() == $class->getName();
    });

    print_r($properties);
}

So now, if you explicitly want to print the parent class properties from a child class, just pass a flag to the print_this() function:

class BMW extends Car{
    public $company;
    public $modal;

    public function __construct(){
        parent::print_this(); // get only this classe's properties
        parent::print_this(true); // get only the parent classe's properties
    }
}

Try

public function print_this()
{
    $reflection = new ReflectionClass(__CLASS__);
    $properties = $reflection->getProperties();

    $propertyValues = [];

    foreach ($properties as $property)
    {
        $propertyValues[$property->name] = $this->{$property->name};
    }

    print_r($propertyValues);
}

You could try something like this:

class Car{
  public $doors;
  public $wheels;
  public $color;
  public $size;

  public function print_this(){
    print_r(new Car());
  }
}

Or this:

class Car{
  public $doors;
  public $wheels;
  public $color;
  public $size;
  public $instance;

  public function __constructor(){
    $this->instance = new Car;
  }

  public function print_this(){
    print_r($this->instance);
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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