PHP中static静态属性和静态方法的调用 Posted on 2019-02-22 | In Develop | PV 1234567891011121314151617181920212223242526272829303132333435363738394041<?phpheader("content-type:text/html;charset=utf-8");class Human{ static public $name = "妹子"; public $height = 180; public $age;// 构造方法public function __construct(){ $this->age = "Corwien"; // 测试调用静态方法时,不会执行构造方法,只有实例化对象时才会触发构造函数,输出下面的内容。 echo __LINE__,__FILE__,'<br>'; } static public function tell(){ echo self::$name;//静态方法调用静态属性,使用self关键词 //echo $this->height;//错。静态方法不能调用非静态属性//因为 $this代表实例化对象,而这里是类,不知道 $this 代表哪个对象 } public function say(){ echo self::$name . "我说话了"; //普通方法调用静态属性,同样使用self关键词 echo $this->height; }}$p1 = new Human();$p1->say(); $p1->tell();//对象可以访问静态方法echo $p1::$name;//对象访问静态属性。不能这么访问$p1->name//因为静态属性的内存位置不在对象里Human::say();//错。say()方法有$this时出错;没有$this时能出结果//但php5.4以上会提示/* 调用类的静态函数时不会自动调用类的构造函数。测试方法,在各个函数里分别写上下面的代码 echo __LINE__,__FILE__,'<br>'; 根据输出的内容,就知道调用顺序了。*/// 调用静态方法,不会执行构造方法,只有实例化对象时才会触发构造函数,输出构造方法里的内容。Human::tell();?> Reward WeChat Pay Alipay Bitcoin Post author: Qin Post link: https://qinhaolei.com/2019/02/22/Develop/2019/PHP中static静态属性和静态方法/ Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.