php中建構函式和解構函式執行過程

來源:互聯網
上載者:User

最近看書,做了個小測試,建構函式和解構函式執行過程簡單瞭解了下,如果出現同一變數作為來自不同的類執行個體,那麼,在php中構造和析構將會如何執行的,實驗了下,順序應該是先構造。單該對象重新被執行個體化後,再次調用構造,同時,上一個執行個體已經沒有用了,它會自動析構,然後用重新被執行個體的對象操作,最後將重新被執行個體的對象析構。
檔案2-1.php

 

Code
 1//Class one
 2class Person
 3{
 4 private  $name;
 5 public $age = 15;
 6 private $conName ;
 7 function __construct($var = '111')
 8 {
 9  $this->weight = '70';
10  $this->conName = $var;
11  echo "建構函式被調用<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/>解構函式調用';
25 }
26}
27//Class Two
28class Student{
29 private $name = 'li';
30 public $age = '30';
31 public $weight = '70';
32}
33
34class Extends_Person extends Person{
35 function Bank(){
36  echo 'person extends banks';
37 }
38}
39
40$e = new Extends_Person();
41$e->set_name('aaa');
42echo $e->get_name()."<BR/>";
43echo $e->weight."<BR/>";
44echo $e->Bank();
45

檔案2-2.php
require_once('2-1.php');
$e = new Person('456');
echo '<BR/>'.$e->age;
exit;

=================
輸出結果:

建構函式被繼承
aaa
70
person extends banks建構函式被繼承

111
解構函式繼承
15
456
解構函式繼承

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.