----- 009-class.php -----
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta http-equiv="content-type" content="text/html; charset=utf-8"> 5 <title>一个PHP网页</title> 6 </head> 7 <body> 8 <pre> 9 <?php 10 echo "当前页面是:", __file__, "<br/>"; 11 class Monitor 12 { 13 private $name = "显示器"; 14 public function iam() 15 { 16 echo "我是", $this->name; 17 } 18 } 19 class A 20 { 21 function foo() 22 { 23 if(isset($this)){ 24 echo ‘$this id defined (‘, get_class($this), ")\n"; 25 } 26 else{ 27 echo ‘$this is not defined‘, "\n"; 28 } 29 } 30 } 31 class B 32 { 33 function bar() 34 { 35 A::foo(); 36 } 37 } 38 echo ‘<h3>$this 变量测试</h3>‘; 39 $a = @new A(); 40 @$a->foo(); 41 @A::foo(); 42 $b = @new B(); 43 @$b->bar(); 44 @B::bar(); 45 ?> 46 </pre> 47 </body> 48 </html>
----- 009-class2.php -----
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta http-equiv="content-type" content="text/html; charset=utf-8"> 5 <title>一个PHP网页</title> 6 </head> 7 <body> 8 <?php 9 function __autoload($class_name) 10 { 11 require_once("009-class.php"); 12 } 13 echo "当前页面是:", __file__, "<br/>"; 14 class CRT extends Monitor 15 { 16 public $name; 17 function CRT() 18 { 19 $this->name = "CRT显示器"; 20 } 21 public function iam() 22 { 23 $the = "name"; 24 echo "我是", $this->$the, "<br/>"; 25 echo "我的祖先是"; 26 parent::iam(); 27 } 28 function __destruct() 29 { 30 echo("<br>这里是析构函数"); 31 } 32 } 33 $crt = new CRT(); 34 $crt->iam(); 35 ?> 36 </body> 37 </html>