PHP constructor and destructor Execution Process

Source: Internet
Author: User

I recently read a book and conducted a small test. The execution process of constructor and destructor is simple. If the same variable appears as an instance of different classes, in PHP, how can we construct and analyze the structure? After testing, the sequence should be constructed first. After the object is re-instantiated, the constructor is called again. At the same time, the previous instance is no longer in use. It will automatically analyze the constructor and then operate on it again, the object structure of the instance will be re-analyzed.
File 2-1.php

 

Code
1 // class one
2 class person
3 {
4 private $ name;
5 Public $ age = 15;
6 private $ conname;
7 function _ construct ($ Var = '000000 ')
8 {
9 $ this-> Weight = '70 ';
10 $ this-> conname = $ var;
11 echo "constructor called <br/> ";
12}
13 function get_name ()
14 {
15 return $ this-> name;
16}
17 function set_name ($ INAME)
18 {
19 $ this-> name = $ INAME;
20}
21 function _ destruct ()
22 {
23 echo '<br/>'. $ this-> conname;
24 echo '<br/> destructor call ';
25}
26}
27 // Class Two
28 class student {
29 private $ name = 'lil ';
30 public $ age = '30 ';
31 public $ Weight = '70 ';
32}
33
34 class extends_person extends person {
35 function Bank (){
36 echo 'person extends bank ';
37}
38}
39
40 $ e = new extends_person ();
41 $ e-> set_name ('aaa ');
42 echo $ e-> get_name (). "<br/> ";
43 echo $ e-> weight. "<br/> ";
44 echo $ e-> Bank ();
45

File 2-2.php
Require_once ('2-1. php ');
$ E = new person ('20140901 ');
Echo '<br/>'. $ e-> age;
Exit;

========================
Output result:

Constructor inherited
Aaa
70
The person extends banks constructor is inherited.

111
Destructor inheritance
15
456
Destructor inheritance

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.