php - Importing methods from other classes?

311

Can I import methods from other classes without using the inheritance of 'extends' from them?

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

Usage,

$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 

Ideally,

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

Is it possible?

EDIT:

I know I can achieve it like this,

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

But I am hoping for importing the methods without retyping them. Is it possible?

114

Answer

Solution:

Yes. traits have been added in PHP 5.4, and they do exactly what you want:

The manual says it beautifully:

A Trait is intended to reduce some limitations of single inheritance by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies

Here's a sample:

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();
179

Answer

Solution:

you can add a __call method in your Boo class:

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

with as result:

$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());

returns:

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

People are also looking for solutions to the problem: php - Add trailing slash to domain name

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.