php 中this 的理解

<?php
    class A {
        public function foo () {
            if(isset($this)) {
                echo "\$this is defined (";
                echo get_class($this);
                echo ")\n";
            } else {
                echo "\$this is not defined .\n";
            }
        }
        
        static public function soo() {
            if(isset($this)) {
                echo "\$this is defined (";
                echo get_class($this);
                echo ")\n";
            } else {
                echo "\$this is not defined .\n";
            }
        }
    }
    
    class B {
        function bar() {
            A::soo();
        }
        
        function t() {
            $x = new A();
            $x->foo();
        }
       static function st () {
            $x = new A();
            $x->foo(); 
       }
    }

    $a = new A(); // output nothing 
    $a->foo(); // $this is defined (A) 
    echo "<br/>";
    A::soo();  // $this is not defined .
    echo "<br/>";
    $b = new B(); // output nothing 
    $b->bar(); //$this is not defined .
    echo "<br/>";
    B::st(); //$this is defined (A) 
    echo "<br/>";
    $x = new B();
    $x->t(); //$this is defined (A) 
    echo "<br/>";

?>

  

posted @ 2013-03-15 16:01  刘牛德  阅读(247)  评论(0)    收藏  举报