php关键词construct和static
1 <?php 2 header("content-type:text/html; charset=utf-8"); 3 /*php中常用的关键字 4 * final 5 * 1.final不能修饰成员属性 6 * 2.final只能修饰类和方法 7 * 作用: 8 * static: 9 1.能修饰成员属性和成员方法,不能修饰类 10 2.是所有成员共享成员属性 11 3.用$p::$静态属性即可调用 12 * const 13 */ 14 /*创建类*/ 15 class preson{ 16 var $name; 17 var $age; 18 var $sex; 19 static $country="china";/*static属性时共享的*/ 20 21 /*创建构造方法*/ 22 function __construct($name="",$age=0,$sex="男"){ 23 $this->name=$name; 24 $this->age=$age; 25 $this->sex=$sex; 26 } 27 function say(){ 28 29 echo"my name is{$this->name};my age is{$this->age};my sex is{$this->sex}.<br/>"; 30 } 31 32 function staticsay(){ 33 34 return self::$country; 35 } 36 } 37 /*实例化对象*/ 38 $p=new preson(); 39 $p->say("lee",24,'男'); 40 $p2=new preson("ling",25,'女'); 41 /*获返回值*/ 42 echo $p->staticsay(); 43 ?>
if you don't try,you will never know!