Answer the question
In order to leave comments, you need to log in
How to get an array of class attributes using get_object_vars on the parent?
Question on topic. The situation is this:
class Catalog extends Table {
private $type;
private $desc;
}
abstract class Table {
public function __toString() {
$vars = get_object_vars();
return implode(', ', $vars);
}
}
Answer the question
In order to leave comments, you need to log in
Melkij +1
parent class will not see private method declared in heirs.
On the fingers of OOP, it works like this:
Catalog->__toString() actually works like this Catalog::parent->__toString() (that is, the function is called in the context of Table)
If you really want it through get_object_vars, then it should be called from heirs. Like your comment or like this:
<?php
class Catalog extends Table {
private $type;
private $desc;
protected $prot;
public $pub;
protected function getMetods() {
$vars = get_object_vars($this->self);
return array_keys($vars);
}
}
abstract class Table {
public $perPub;
protected $perProt;
private $perPriv;
protected $self;
abstract protected function getMetods();
public function __toString() {
return implode(', ', $this->getMetods());
}
}
$o=new Catalog();
echo (string) $o;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question