a question about __call($method, $arguments) function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
aneuryzma
Forum Contributor
Posts: 106
Joined: Sat May 17, 2008 7:03 am

a question about __call($method, $arguments) function

Post by aneuryzma »

Hello,

I've a question about the __call method in PHP 5.

If I have 2 aggregated instances in my class. For example:

Code: Select all

public function _ _construct( ) {
        $this->class1 = new Class1;
        $this->class1 = new Class1;
    }

And these classes have methods with the same name. How does the _call function decides which method to invoke ?

Code: Select all

//aggregation
  public function __call($method, $arguments) {
 
        if (method_exists($this->aggregatedInstance, $method)) {
        
            return call_user_func_array(array($this->address, $method), $arguments);
        }
    }
I hope the question is clear.
thanks
Last edited by Benjamin on Sun May 03, 2009 12:51 pm, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: a question about __call($method, $arguments) function

Post by requinix »

For your first code, there's only one class. A variable can't have multiple values so $class1 will be whatever the results of the second new Class1. The first one is thrown away.
aneuryzma wrote:How does the _call function decides which method to invoke ?
__call is your function. You decide how it works.

Your question is puzzling. It's like you're holding back one too many details.
aneuryzma
Forum Contributor
Posts: 106
Joined: Sat May 17, 2008 7:03 am

Re: a question about __call($method, $arguments) function

Post by aneuryzma »

Hi,

yes sorry, I've written twice class1 instance 1, but I wanted to write class2 and instance 2 in the second row.

if I want to define 2 functions:

function1 -> callng instance1->getName
and
function2 -> calling instance2 -> getName

how should I do ?

thanks
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: a question about __call($method, $arguments) function

Post by requinix »

__call only provides a method name and it's arguments. So one of those needs to mention which class's method should be called.

Hmm... Without really knowing your situation, my first idea would be code like this.

Code: Select all

function __call($method, $args) {
    $class = shift($args); // get the first argument and remove it from the list
    // then use $class to determine which object to use
    // maybe like
    $classvar = "class" . $class; // if $class=1 then $classvar="class1"
    return call_user_func_array(array($this->$classvar, $method), $args);
}
 
// call $obj->class1->method(a, b, c)
$obj->method(1, "a", "b", "c");
// call $obj->class5->another(x, y, z)
$obj->another(5, "x", "y", "z");
But I'm pretty sure there's a better way of doing this. What are you using this class for? How?
aneuryzma
Forum Contributor
Posts: 106
Joined: Sat May 17, 2008 7:03 am

Re: a question about __call($method, $arguments) function

Post by aneuryzma »

thanks for reply, I'm studying it, I just want to understand how it works.

Your example is clear. This is my question:

"what's happen if you have 2 aggregated classes class A and B inside your main class and

you want to call a function of A when function myClass -> func1 is called
and a function of B for function myClass -> func2 is called

For example:

myClass -> func1() //this calls the function A->getName();
myClass -> func2() //this call the function B-> getName();

what should I write inside __call() method if I want to have this behavior ?

thanks
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: a question about __call($method, $arguments) function

Post by requinix »

You look at $method (in my code) to decide what to do.
If $method == "func1" then you call class 1's method; if $method == "func2" then you call class 2's method.
Post Reply