Interpret Order

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
jamInTheValleys
Forum Newbie
Posts: 4
Joined: Sat Jan 09, 2010 2:18 pm

Interpret Order

Post 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
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: Interpret Order

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Interpret Order

Post 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']);
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: Interpret Order

Post 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
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Interpret Order

Post 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');
There are 10 types of people in this world, those who understand binary and those who don't
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Re: Interpret Order

Post 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
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Interpret Order

Post by josh »

Yes that is how anonymous functions work (I know them from doing javascript, not C). This is new in php5.3
Post Reply