Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从其他类导入方法?_Php_Oop_Inheritance_Multiple Inheritance_Php 5.5 - Fatal编程技术网

Php 从其他类导入方法?

Php 从其他类导入方法?,php,oop,inheritance,multiple-inheritance,php-5.5,Php,Oop,Inheritance,Multiple Inheritance,Php 5.5,我可以从其他类导入方法而不使用从它们继承的“extends”吗 class Foo { public function fooMethod() { return 'foo method'; } } class Too { public function tooMethod() { return 'too method'; } } class Boo { public $foo; public $too;

我可以从其他类导入方法而不使用从它们继承的“extends”吗

class Foo
{
    public function fooMethod() {
        return 'foo method';
    } 
}

class Too
{
    public function tooMethod() {
        return 'too method';
    } 
}

class Boo
{
    public $foo;
    public $too;

    public function __construct()
    {
        $this->foo = new Foo();
        $this->too = new Too();
    }
}
用法

$boo = new Boo();

var_dump($boo->foo->fooMethod()); // string 'foo method' (length=10)
var_dump($boo->too->tooMethod()); // string 'too method' (length=10)
var_dump($boo->fooMethod()); // Fatal error: Call to undefined method Boo::fooMethod() in 
var_dump($boo->tooMethod()); //Fatal error: Call to undefined method Boo::tooMethod() in 
理想情况下

var_dump($boo->fooMethod()); // string 'foo method' (length=10)
var_dump($boo->tooMethod()); // string 'too method' (length=10)
可能吗

编辑:

我知道我可以做到这一点

class Boo
{
    public $foo;
    public $too;

    public function __construct()
    {
        $this->foo = new Foo();
        $this->too = new Too();
    }

    public function fooMethod() {
        return $this->foo->fooMethod();
    }

    public function tooMethod() {
        return $this->too->tooMethod();
    }
}

但是我希望导入方法,而不必重新键入它们。有可能吗?

您可以在Boo类中添加一个调用方法:

public function __call($method, $args)
{
    if (method_exists($this->foo, $method)) {
        return call_user_func_array(array($this->foo, $method), $args);
    }
    else if (method_exists($this->too, $method)) {
        return call_user_func_array(array($this->too, $method), $args);
    }
    else {
        throw new Exception("Unknown method " . $method);
    }
}
结果:

$boo = new Boo();

var_dump($boo->foo->fooMethod());
var_dump($boo->too->tooMethod());
var_dump($boo->fooMethod());
var_dump($boo->tooMethod());
var_dump($boo->newMethod());
返回:

string(10) "foo method"
string(10) "too method"
string(10) "foo method"
string(10) "too method"
PHP Fatal error:  Uncaught exception 'Exception' with message 'Unknown method newMethod' in /tmp/call.php:38
Stack trace:
#0 /tmp/call.php(49): Boo->__call('newMethod', Array)
#1 /tmp/call.php(49): Boo->newMethod()
#2 {main}
  thrown in /tmp/call.php on line 38

您可以在Boo类中添加_调用方法:

public function __call($method, $args)
{
    if (method_exists($this->foo, $method)) {
        return call_user_func_array(array($this->foo, $method), $args);
    }
    else if (method_exists($this->too, $method)) {
        return call_user_func_array(array($this->too, $method), $args);
    }
    else {
        throw new Exception("Unknown method " . $method);
    }
}
结果:

$boo = new Boo();

var_dump($boo->foo->fooMethod());
var_dump($boo->too->tooMethod());
var_dump($boo->fooMethod());
var_dump($boo->tooMethod());
var_dump($boo->newMethod());
返回:

string(10) "foo method"
string(10) "too method"
string(10) "foo method"
string(10) "too method"
PHP Fatal error:  Uncaught exception 'Exception' with message 'Unknown method newMethod' in /tmp/call.php:38
Stack trace:
#0 /tmp/call.php(49): Boo->__call('newMethod', Array)
#1 /tmp/call.php(49): Boo->newMethod()
#2 {main}
  thrown in /tmp/call.php on line 38
对。已添加到PHP 5.4中,它们完全满足您的要求:

手册漂亮地说:

特征旨在通过启用 开发人员可以在多个独立的类中自由地重用方法集 不同的类层次结构

以下是一个示例:

trait FooTrait {
  public function fooMethod() {
        return 'foo method';
  } 
}

trait TooTrait {
    public function tooMethod() {
        return 'too method';
    } 
}

class Foo
{
    use FooTrait;
}

class Too
{
    use TooTrait;
}

class Boo
{
    use FooTrait;
    use TooTrait;
}

$a = new Boo;
echo $a->fooMethod();
echo $a->tooMethod();
对。已添加到PHP 5.4中,它们完全满足您的要求:

手册漂亮地说:

特征旨在通过启用 开发人员可以在多个独立的类中自由地重用方法集 不同的类层次结构

以下是一个示例:

trait FooTrait {
  public function fooMethod() {
        return 'foo method';
  } 
}

trait TooTrait {
    public function tooMethod() {
        return 'too method';
    } 
}

class Foo
{
    use FooTrait;
}

class Too
{
    use TooTrait;
}

class Boo
{
    use FooTrait;
    use TooTrait;
}

$a = new Boo;
echo $a->fooMethod();
echo $a->tooMethod();

你需要有Foo和Too作为课程吗?如果不是的话,我想你需要的是一个重新输入——它被称为分派。你需要有Foo和Too作为类吗?如果不是的话,我想你需要的是一个重新输入,它叫做分派。@tealou:缺点是你需要PHP5.4。很快,它将与PHP5.5中的其他酷东西一起投入生产:)我需要寻找一个托管提供商,至少支持PHP5.4那么!但我认为PHP5.4现在对于大多数托管提供商来说是相当普遍的:D@tealou:缺点是您需要PHP5.4。很快,它将与PHP5.5中的其他酷东西一起投入生产:)我需要寻找一个托管提供商,至少支持PHP5.4那么!但是我认为PHP5.4现在对于大多数主机提供商来说是非常普遍的:DI会补充说,这种行为会使您的应用程序更难调试,特别是在查找哪个方法属于哪个类时。PHP5.4之前-这是唯一的解决方案:)感谢padawin的回答。很高兴知道这一点-
这种行为会使您的应用程序更难调试。
!:)然而,我要补充的是,这种行为会使您的应用程序更难调试,特别是在查找哪个方法属于哪个类时。PHP 5.4之前-这是唯一的解决方案:)感谢padawin的回答。很高兴知道这一点-
这种行为会使您的应用程序更难调试。
!:)