php的子类能够调用父类的方法么?怎么调用啊

解决方案 »

  1.   

    <?php 
    class A{
        function test(){
        echo "hello!";
        }
    }
    class B extends A{//若A类和B类不在同一文件中 请包含后(include)再操作
        function test2(){
            parent::test();//子类调用父类方法
            echo "test2";
        }
    }
    $a = new B();
    $a->test();//hello!
    $a->test2();//hello!test2
    ?>
      

  2.   


    parent::test();//子类调用父类方法  to$this->test(); // as the same
      

  3.   

    class A{
        function test(){
               echo "hello!<hr/>";
        }
    }
    class B extends A{//若A类和B类不在同一文件中 请包含后(include)再操作
        function test2(){
       parent::test();//子类调用父类方法
        }
        function test()
        {
         echo "b2_test_hello<hr/>";
        }
    }
    $a = new B();
    $a->test();//hello!
    $a->test2();//hello!test2/** parent::test();一直是调用父类的。 而 $this->test() 则是子类没有申明时调用父类的方法,子类有这个方法时,则调用的是子类自己的方法
    $a->test();//b2_test_hello
    --------------------------------------------------------------------------------
    $a->test2();//hello**/
      

  4.   

    子类内部调用父类方法用parent:: 自己的用self::