PHP将类中的方法调用到另一个类的子类中
请帮帮我。 我有一门课:PHP将类中的方法调用到另一个类的子类中,php,oop,php-5.3,Php,Oop,Php 5.3,请帮帮我。 我有一门课: class Foo() { public function{ if($var = x){ do this; } else { do that; } } } 另一类: class B extends A() { public function { #need to import method from Foo
class Foo() {
public function{
if($var = x){
do this;
}
else {
do that;
}
}
}
另一类:
class B extends A() {
public function {
#need to import method from Foo
#to execute on a varible in this class
}
}
有人能帮我解决这个问题吗。语言是PHPclass Foo{
class Foo() {
public static function test {
if($var = x) {
do this;
}
else {
do that;
}
}
}
class B extends A() {
private $variable = 2;
public function test {
Foo::test($this->variable);
}
}
受保护的$var;
函数构造($var){
$this->var=$var;
}
功能测试(){
echo“来自Foo类的方法测试
”;
如果($this->var==NULL){
echo“Test==Null
”;
}
否则{
echo“Test!==Null
”;
}
}
}
类Ftt扩展了Foo{
受保护$1;
函数构造($var,$var1){
$this->var1=$var1;
父项::_构造($var);
}
功能测试(){
父::test();
echo“来自类Ftt的方法测试扩展了Foo”;
echo“使用$this->var1
”;
}
}
$ftt=新ftt('notnull','var1');
$ftt->test('notnull');
$foo=新foo(“”);
$foo->test();
你能描述一下导入是什么意思吗?如果B扩展了A,那么你可以通过简单地使用$B=new B()调用该方法$b->methodDefinedInA($var\u from\u b)
对不起,我的意思是我需要从类Foo调用一个方法如果你说你想从b中的Foo访问一个函数,你可以在b中创建一个Foo的新实例(或者静态调用它,如果适用的话),或者让b扩展Foo。b是在扩展Foo还是Foo是一个单独的类?如果是这样,您可以使foo函数成为静态的,并在B中静态地调用它。比如Foo::Function();
class Foo {
protected $var;
function __construct($var) {
$this->var = $var;
}
function test() {
echo "Method Test from class Foo<br>";
if ($this->var == NULL) {
echo "Test = = Null <br>";
}
else {
echo "Test != = Null <br>";
}
}
}
class Ftt extends Foo {
protected $var1;
function __construct($var, $var1) {
$this->var1 = $var1;
parent::__construct($var);
}
function test() {
parent::test();
echo "Method Test from class Ftt extends Foo";
echo " with $this->var1 <br>";
}
}
$ftt = new Ftt('notnull', 'var1');
$ftt->test('notnull');
$foo = new Foo('');
$foo->test();