Whether to first execute the abstract method in the abstract type in PHP before executing other methods

Source: Internet
Author: User
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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.