Does the abstract method in the abstract class in PHP first execute the abstract method before executing other methods? Rtabstract & nbsp; class & nbsp; Node & nbsp; {private & nbsp; $ debugMessages; public & nbsp; whether to first execute the abstract method in the abstract class in funct PHP, and then execute other methods?
Rt
abstract class Node {
private $debugMessages;
public function __construct() {
$this->debugMessages = array();
$this->debug(__CLASS__.' constructor called.');
}
public function __destruct() {
$this->debug(__CLASS__.' destructor called.');
$this->dumpDebug();
}
protected function debug($msg) {
$this->debugMessages[] = $msg;
}
private function dumpDebug() {
echo implode('
', $this->debugMessages);
}
public abstract function getView();
}
class ForumTopic extends Node {
private $debugMessages;
public function __construct() {
parent::__construct();
$this->debug(__CLASS__.' constructor called.');
}
public function __destruct() {
$this->debug(__CLASS__.' destructor called.');
parent::__destruct();
}
public function getView() {
return 'This is a view into '.__CLASS__.'
';
}
}
$forum = new ForumTopic();
echo $forum->getView();
Execution result:
This is a view into ForumTopic
Node constructor called.
ForumTopic constructor called.
ForumTopic destructor called.
Node destructor called.
But how can I call and execute getView () without the new ForumTopic ()? Php destructor constructor function class
------ Solution --------------------
Execution sequence:
ForumTopic ::__ construct ()
Node: :__ construct ()
Node: debug ()
ForumTopic: debug ()
ForumTopic: getView ()
ForumTopic ::__ destruct ()
ForumTopic: debug ()
Node: :__ destruct ()
Node: debug ()
Node: dumpDebug ()
------ Solution --------------------
I don't know what you want to say. if you want to call it first, you can execute it.
You first execute echo $ forum-> getView (); so first output: This is a view into ForumTopic
When the object is destroyed, the parent class destructor prints all the data in $ debugMessages. As follows:
Node constructor called.
ForumTopic constructor called.
ForumTopic destructor called.
Node destructor called.
_ CLASS _ indicates the current CLASS.
Get_class ($ obj) refers to the class of instance $ obj
------ Solution --------------------
If you
Protected function debug ($ msg ){
$ This-> debugMessages [] = $ msg;
}
Change
Protected function debug ($ msg ){
Echo $ msg;
}
You can see the real execution order
------ Solution --------------------
Reference:
What are the essential differences between _ CLASS _ and get_class? It seems that one of them always points to one class, and the other points to different classes based on different class domains.
One is a function, and the other is a variable. Get_class () requires object parameters.
The example you wrote is reload.
"But there is no new ForumTopic (). how can I call and execute getView ()? "You didn't understand this sentence. how can this problem conflict with your example?