P
P
Platton2016-07-24 11:57:52
PHP
Platton, 2016-07-24 11:57:52

How to get the name of a subclass?

Hello. Tell me how you can assign a string to the $now property of the Chief class - the name of the subclass (A or B or C) in which an undefined property is currently called . That is, how can the Chief class know the names of its subclasses in which an undefined property is called.

error_reporting(E_ALL);

class Chief 
{
  public $now;
  public function __get($name){
    return $name;
  }
}

class A extends Chief
{
  public function perf(){
    return $this->pr_a;
  }
}

class B extends Chief
{
  public function perf(){
    return $this->pr_b;
  }
}

class C extends Chief
{
  public function perf(){
    return $this->pr_c;
  }
}

class Closing extends Chief
{
  public $all = array('A','B','C');
  public function perf(){
    foreach($this->all as $v){
      $aught = new $v;
      echo $aught->perf().'<br>';
    }
  }
}

$res = new Closing();
print $res->perf();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vdem, 2016-07-24
@vdem

A class cannot know, an instance of a class can know .
get_class($this);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question