PHP call function from parent in child best practice -
i have trick question regarding php calling function parent in child class. have 3 scenarios, , want pros , cons.
<?php class test{ private $var ; public function __construct(){ $this->var = 'hello world'; } public function output(){ echo $var.'<br>'; } } //scenario 1 class test1 extends test{ public function __construct(){ parent::__construct(); } public function say(){ parent::output(); } } //scenario 2 class test2 extends test{ public function __construct(){ test::__construct(); } public function say(){ test::output(); } } //scenario 3 class test3 extends test{ private $handle ; public function __construct(){ $this->handle = new test(); } public function say(){ $this->handle->output(); } } //finally can call 3 cases 1 of below codes $test1 = new test1(); $test1->say(); //or $test2 = new test2(); $test2->say(); //or $test3 = new test3(); $test3->say(); ?>
is there best practice or there of 3 scenarios better other?
thank in advance.
1) correct
2) incorrect call method static method.
3) not have sense extend , create in constructor.
Comments
Post a Comment