我如何在OOP中实现?

时间:2023-01-26 20:46:03
// 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 ";
  }
}

I DID for now like this:

我现在就像这样:

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");
    }

}

BUT I want to class B have all the construct of class A?

但是我想要B级的所有A级结构?

so when I DO on class C (just something like this)

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

class C extends B {

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

}

Anybody could tell me how can implement this?

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

2 个解决方案

#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");
    }

}

#2


2  

don't know php that good, but are you searching for this...

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

class A {
}

class B extends A{
}

class C extends B{
}

or did i miss something?

还是我错过了什么?

#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");
    }

}

#2


2  

don't know php that good, but are you searching for this...

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

class A {
}

class B extends A{
}

class C extends B{
}

or did i miss something?

还是我错过了什么?