静态与非静态的区别
1. 设置出发点:静态是给类使用,非静态是给对象使用
2. 对象不能访问静态属性(对象访问属性只会去对象的内存区域(堆)找,而静态属性在代码区),对象能够访问静态方法(静态方法和普通方法都在代码区里)
3. 类能够访问普通方法(方法里面没有使用$this),类不能访问普通属性(属性是在对象空间里)
<pre name="code" class="php"><?php
class Person
{
public $name;
public $age;
public static $count = 0; // 只能在类的内部才能访问
function __construct($name,$age)
{
$this->name = $name;
$this->age = $age;
self::$count++;
}
public static function getCount() {
return self::$count;
}
}
$tom = new Person("tom",19);
$jack = new Person("jack",20);
// $count = $jack->getCount();
echo Person::$count;
?>