Jump to content

Interpret Order


jamInTheValleys

Recommended Posts

Hello all,

 

I'm wondering if I have this:

 

$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

Link to comment
https://forums.phpfreaks.com/topic/187869-interpret-order/
Share on other sites

That's what I thought :)

 

I'm just building a function that builds inputs based off a type parameter. I don't mean the type attribute of the input tag but stuff like address, money, number, whole number, etc. I just thought it would be novel if the function was called when $switch['one'] was called.

 

The idea that I was having was that if the number of input types were very large then $switch would be small and the call to $switch['one'] would be fast...

 

I'm trying to avoid parsing what will end up being a very large switch statement.

 

 

Link to comment
https://forums.phpfreaks.com/topic/187869-interpret-order/#findComment-991914
Share on other sites

I had a little brainwave while replying. For anyone interested I've done this instead:

 

include_once('input/'.$type.'.php');
$className = f(type);
return x(new $className())->Render($name, $value);

 

$type is always a string in lower case words seperated by underscores.

f() initialises each word and gets rid of the underscores. i.e. input_type -> InputType

x() just takes the parameter and returns it so I can call Render() in one line.

 

This means I need a php file for every input type but I reckon that'll be better (faster/less overhead) and more manageable than a very long switch statement...

Link to comment
https://forums.phpfreaks.com/topic/187869-interpret-order/#findComment-991926
Share on other sites

You mean so I can:

 

function __autoload($className) {
    include_once('input/'.$className. '.php');
}

 

?

 

I think it reads better to have the include next to the instantiation.

 

Unless you meant for error capturing when there isn't an associated php file for a type... :)

Link to comment
https://forums.phpfreaks.com/topic/187869-interpret-order/#findComment-991938
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.