PHP에 관한 PHP의 하위 클래스에 대한 함수 호출에 관한 트릭 질문이 있습니다. 우리는 3 가지 시나리오를 가지고 있으며 장단점을 원합니다.자식 모범 사례에서 부모로부터 PHP 호출 함수
<?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 I can call any 3 cases by one of the below codes
$test1 = new test1();
$test1->say();
//or
$test2 = new test2();
$test2->say();
//or
$test3 = new test3();
$test3->say();
?>
다른 시나리오보다 좋은 세 가지 시나리오가 있습니까?
미리 감사드립니다.
상위를 사용하는 것이 좋습니다. – Ares