Page 1 of 1
a question about __call($method, $arguments) function
Posted: Sun May 03, 2009 11:53 am
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
Re: a question about __call($method, $arguments) function
Posted: Sun May 03, 2009 3:22 pm
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.
Re: a question about __call($method, $arguments) function
Posted: Sun May 03, 2009 3:28 pm
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
Re: a question about __call($method, $arguments) function
Posted: Sun May 03, 2009 4:21 pm
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?
Re: a question about __call($method, $arguments) function
Posted: Mon May 04, 2009 12:17 am
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
Re: a question about __call($method, $arguments) function
Posted: Mon May 04, 2009 1:23 am
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.