我如何在OOP中实现?

时间:2011-05-17 13:49:50

标签: php oop inheritance

// A is the core class

class A{
   public $lang;
   function sayhi($name){echo "Hi".$name;}
   function speak(){echo "Can Speak".$this->lang;}
}

class B {

  function TODO(){
       echo  " Go to work ";
  }
}

我现在就这样说:

class C extends B {

    function TODO(){
    //more implement here 

    $a = new A();// here I created an instance.
        // do  any  actions for A
    $a->sayhi("Newbie");
    }

}

但我想B班有A级的所有结构吗?

所以当我在C级上做(就像这样)

class C extends B {

    function TODO(){
    //more implement here 
     // I wish I can 
       sayhi("Newbie");
    }

}

任何人都可以告诉我如何实现这个?

2 个答案:

答案 0 :(得分:2)

不知道php那么好,但是你在寻找这个......

class A {
}

class B extends A{
}

class C extends B{
}

还是我错过了什么?

答案 1 :(得分:1)

class A{
   public $lang;
   function sayhi($name){echo "Hi".$name;}    //here you missed a semicolon
   function speak(){echo "Can Speak".$this->lang;}
}

class B extends A {

  function TODO(){
      echo  " Go to work ";
  }
}

class C extends B {

    function TODO(){
        $this->sayhi("Newbie");
    }

}