[PHP] 纯文本查看 复制代码
<?php
header('content-type:text/html;charset=utf-8');
class Human{
static public $head=1;
public function easyeat(){
echo '普通方法吃饭<br />';
}
static public function eat(){
echo '静态方法吃饭<br />';
}
public function intro(){
echo $this->name;
}
}
Error_reporting(E_ALL|E_STRICT);
//此时没有对象!方法可以执行
Human::eat();
/*
以下方法easyeat是一个非静态方法,就由对象来调用,但,用类来调用此方法来也可以执行,而严格状态下,此方法会执行,同时报错,
Strict Standards: Non-static method Human::easyeat() should not be called statically in D:\application\PHPnow-1.5.6\htdocs\yan18\types\staticfun.php on line 32
*/
Human::easyeat();
/*
接上,从逻辑来理解,如果用类名静态调用非静态(普通)方法
比如:intro()
那么,这个$this是指哪个对象呢??
因此会报错,因为找不到对象!
Fatal error: Using $this when not in object context in D:\application\PHPnow-1.5.6\htdocs\yan18\types\staticfun.php on line 23
*/
Human::intro();
/*
如上分析,其实,非静态方法,是不能由类名静态调用的,但目前,php中的面向对象检测不够严格,只要静态方法中没有$this关键字,就会转化成静态方法来处理!
*/
$li=new Human();
$li->eat();
?>