- <?PHP
- class A {
- protected $name = 'A';
- static $alias = 'a';
- const HASH = 'md5';
- public function dd() {
- echo $this->name; echo '--';
- echo static::$alias; echo '--'; // 后期静态绑定
- echo static::HASH; echo '--'; // 后期静态绑定
- echo self::$alias; echo '--';
- echo self::HASH; echo '--';
- var_dump(new self); echo '--';
- var_dump($this); echo '--';
- var_dump(new static); echo '<br>'; // 后期静态绑定
- }
- public static function who() {
- echo __CLASS__;
- echo '[ This is A ]'; echo '<br>';
- }
- public static function test() {
- self::who();
- }
- public static function test2() {
- static::who(); // 后期静态绑定
- }
- public static function getInstance() {
- var_dump(new self); echo '--';
- var_dump(new static); echo '<br>'; // 后期静态绑定
- }
- }
- class B extends A {
- protected $name = 'B';
- static $alias = 'b';
- const HASH = 'sha1';
- public static function who() {
- echo __CLASS__;
- echo '[ This is B ]'; echo '<br>';
- }
- }
- class C extends B {
- public static function who() {
- echo __CLASS__;
- echo '[ This is C]'; echo '<br>';
- }
- }
- (new A)->dd(); // A--a--md5--a--md5--object(A)#2 (1) {
- ["name":protected]=> string(1) "A"
- } --object(A)#1 (1) {
- ["name":protected]=> string(1) "A"
- } --object(A)#2 (1) {
- ["name":protected]=> string(1) "A"
- }
- (new B)->dd(); // B--b--sha1--a--md5--object(A)#2 (1) {
- ["name":protected]=> string(1) "A"
- } --object(B)#1 (1) {
- ["name":protected]=> string(1) "B"
- } --object(B)#2 (1) {
- ["name":protected]=> string(1) "B"
- }
- A::who(); // A [ This is A ]
- B::who(); // B [ This is B ]
- A::test(); // A [ This is A ]
- B::test(); // A [ This is A ]
- A::test2(); // A [ This is A ]
- B::test2(); // B [ This is B ]
- C::test2(); // C [ This is C]
- A::getInstance(); // object(A)#1 (1) {
- ["name":protected]=> string(1) "A"
- } --object(A)#1 (1) {
- ["name":protected]=> string(1) "A"
- }
- B::getInstance(); // object(A)#1 (1) {
- ["name":protected]=> string(1) "A"
- } --object(B)#1 (1) {
- ["name":protected]=> string(1) "B"
- }
总结说明:
来源: http://www.bubuko.com/infodetail-3013548.html