M
M
Max Ba2018-12-10 10:12:23
PHP
Max Ba, 2018-12-10 10:12:23

Do I need to declare a property in a descendant?

The question may be quite stupid, but still. Tell. In this example, is it necessary to declare a property on the heir?

class Company{

  protected $company_id = NULL;

  //обязательно нужно указать ID организации
  public function setCompany($id){
    $this->company_id = $id;
  }
  
  public function getInfo(){
    return $this->company_id;
    //... return data array company...
  }
}

class Department extends Company{

  protected $company_id = NULL; //вот тут нужно?

  public function test(){
    return $this->getInfo();
  }
}

//$a1 = new Company();
//$a1->setCompany(100);

$a2 = new Department();
$a2->setCompany(200);
echo $a2->test();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2018-12-10
@alexey-m-ukolov

No, it doesn `t need.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question