I
I
Ivan Antonov2015-08-11 15:36:02
PHP
Ivan Antonov, 2015-08-11 15:36:02

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

Addition: I wanted to move the repeating code to the parent class, but here's the problem, get_object_vars returns the properties of not Catalog, but Table. Maybe someone can suggest a good solution.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
nonlux, 2015-08-11
@antonowano

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;

S
Sergey Zelensky, 2015-08-11
@SergeyZelensky-Rostov

What code do you have in this case?

M
Melkij, 2015-08-11
@melkij

php.net/manual/en/function.get-object-vars.php
private - not accessible for the parent class.
All sorts of strange things are usually done by reflection.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question