public $name1; public $name2; public $name3; public $name4; /** * [printThis description] * @return [type] [description] */ public function printThis() { // var_dump($this); // var_dump(new $this); // var_dump(new $this); // var_dump(__CLASS__); // var_dump(get_class()); // var_dump(new self); // var_dump(new static ); // var_dump(new static ); $this->name1 = new $this(); $this->name2 = new static(); $this->name3 = new self(); $this->name4 = new Obj(); } } class OtherObj extends Obj { } // $this 是指对象的类名,即被实例化的类名 $a = new OtherObj(); $a->printThis(); var_dump($a);
<?php class Obj { // 初始值不能是表达式 public static $a = 0; public function getAdd2() { return self::$a += 2; } } // 在父类中重写静态类 // 子类和父类的静态成员变量保存不同的值 class OtherObj extends Obj { public static $a = 0; public function getAdd3() { echo parent::getAdd2(); return self::$a += 3; } } $obj = new Obj(); $obj2 = new Obj(); echo $obj->getAdd2(); echo $obj2->getAdd2(); $otherObj = new OtherObj(); echo $otherObj->getAdd3(); echo $otherObj->getAdd3();