jamInTheValleys Posted January 9, 2010 Share Posted January 9, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/187869-interpret-order/ Share on other sites More sharing options...
wildteen88 Posted January 9, 2010 Share Posted January 9, 2010 Would php call the function $this->RenderOne() on assignment of $switch Correct What are you trying to achive? Quote Link to comment https://forums.phpfreaks.com/topic/187869-interpret-order/#findComment-991895 Share on other sites More sharing options...
jamInTheValleys Posted January 9, 2010 Author Share Posted January 9, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/187869-interpret-order/#findComment-991914 Share on other sites More sharing options...
jamInTheValleys Posted January 9, 2010 Author Share Posted January 9, 2010 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... Quote Link to comment https://forums.phpfreaks.com/topic/187869-interpret-order/#findComment-991926 Share on other sites More sharing options...
Mchl Posted January 9, 2010 Share Posted January 9, 2010 Seems like you should take a look into autoloading: http://php.net/manual/en/language.oop5.autoload.php Quote Link to comment https://forums.phpfreaks.com/topic/187869-interpret-order/#findComment-991930 Share on other sites More sharing options...
jamInTheValleys Posted January 9, 2010 Author Share Posted January 9, 2010 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... Quote Link to comment https://forums.phpfreaks.com/topic/187869-interpret-order/#findComment-991938 Share on other sites More sharing options...
Mchl Posted January 9, 2010 Share Posted January 9, 2010 Whatever suits you best. It's usually easier not having to worry about including at all. Just instantiate a class and PHP will include the file with it for you. Quote Link to comment https://forums.phpfreaks.com/topic/187869-interpret-order/#findComment-991942 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.