Show only properties of the parent class in the child class using $ this in the parent class

I have the following two classes. BMW class extends car class.

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 the above code, when I access the method of the parent class from the constructor using the method parent::print_this()and inside print_this(), I have print_r($this)that prints all the properties (properties of the parent and child classes) Now, what I want is print_r(parent::print_this());to output only the parent properties of the class in the child class? Can someone help me with this?

+4
source share
3 answers

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 properties, even when called from a child:
        print_r($class->getProperties());
    }
}

:

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

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

:

, , print_this(), BMW, BMW , print_this() BMW , .

-: / . ( ), print_this() Car :

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);
}

, , print_this():

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

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

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);
}
0

You can try something like this:

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

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

Or that:

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);
  }
}
-1
source

All Articles