Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.