Page 1 of 1

Interpret Order

Posted: Sat Jan 09, 2010 2:36 pm
by jamInTheValleys
Hello all,

I'm wondering if I have this:

Code: Select all

 
$switch = array(
    'one' => $this->RenderOne()
    ,'two' => $this->RenderTwo()
);
 
Would php call the function $this->RenderOne() on assignment of $switch or does it call the function when $switch['one'] is used?

I'd like it if the function is called when $switch['one'] is used.

I know I can easily use a switch statement to get the desired behaviour. I was just curious as to how it got interpreted in this case.

Thanks in advance

Re: Interpret Order

Posted: Sat Jan 09, 2010 4:23 pm
by omniuni
Neither, I don't think.

I believe it would simply assign the return value of the function into the entry of the array. If the function does not return anything, I think PHP would simply assign the value "null".

That said, I believe if you had your function do something and return true, it would do it, return true, and you could easily use that to check if a set of functions worked.

I do not think, however, that you can trigger the function by calling the array; you'll simply find the result of the function from when it was assigned.

Re: Interpret Order

Posted: Sun Jan 10, 2010 12:42 pm
by AbraCadaver
omniuni wrote:Neither, I don't think.

I believe it would simply assign the return value of the function into the entry of the array. If the function does not return anything, I think PHP would simply assign the value "null".

That said, I believe if you had your function do something and return true, it would do it, return true, and you could easily use that to check if a set of functions worked.

I do not think, however, that you can trigger the function by calling the array; you'll simply find the result of the function from when it was assigned.
Correct. To do this you would either need to quote the method calls and then when you need them use the array item as a variable function or eval the array item:

Code: Select all

$switch = array(
    'one' => '$this->RenderOne',
    'two' => '$this->RenderTwo',
);
$func = $switch['one'];
$func();

Code: Select all

$switch = array(
    'one' => '$this->RenderOne();',
    'two' => '$this->RenderTwo();',
);
eval($switch['one']);

Re: Interpret Order

Posted: Mon Jan 11, 2010 4:40 am
by Jenk
To do what is asked of in the OP, you'll need a callback:

Code: Select all

$switch = array(
    "one" => function ($aValue) { echo $aValue; },
    "two" => function ($aValue) { echo $aValue + 10; }
);
 
 
$switch["one"](1);
echo "\n";
$switch["two"](2);
Output:

Code: Select all

1
12

Re: Interpret Order

Posted: Mon Jan 11, 2010 6:23 am
by VladSun
Probably using traditional callbacks is a better idea for now:

Code: Select all

class MyClass
{
    function a1 ()
    {
        echo '1';
    }
    function a2 ()
    {
        echo '2';
    }
 
    function mapper($param)
    {
        $switch = array
        (
            "one" => array($this, 'a1'),
            "two" => array($this, 'a2'),
        );
 
        call_user_func($switch[$param]);
    }
}
 
$a = new MyClass();
 
$a->mapper('two');

Re: Interpret Order

Posted: Mon Jan 11, 2010 9:02 am
by Charles256
Don't suppose it's possible to assign it the address of the function and then when you call the array you can pass the function parameters in since it has the memory address assigned to it and acts as the function as it would in c or is that too crunk? :-D

Re: Interpret Order

Posted: Mon Jan 11, 2010 7:17 pm
by josh
Yes that is how anonymous functions work (I know them from doing javascript, not C). This is new in php5.3