gpt4 book ai didi

php - 如何在 OOP 中实现?

转载 作者:行者123 更新时间:2023-12-04 06:26:00 25 4
gpt4 key购买 nike

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

}

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

最佳答案

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

}

关于php - 如何在 OOP 中实现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6031775/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com