AlexElkins Posted September 25, 2014 Share Posted September 25, 2014 Hello, I need to create a variable that also has a function when invoked, much like the OOP __invoke magic method, but with a variable rather than a class object. #1: Is this possible? Consider the situation: $parent->child->variable = "test"; I also want to run a SQL query using $parent->child->variable('= 2'); where this function would look like function $this->variable($params) { return mysql_query('SELECT FROM `parent`.`child` WHERE `variable`' . $params); } I already know about safety, etc., so I don't need help on that front. I just need to know if a variable can also be used as a function via its own __invoke magic method or something similar? Quote Link to comment https://forums.phpfreaks.com/topic/291285-invoke-variable-method/ Share on other sites More sharing options...
Barand Posted September 25, 2014 Share Posted September 25, 2014 do you mean like this? <?php function test1() { echo "Test 1<br>"; } function test2() { echo "Test 2<br>"; } for ($i=0; $i<4; $i++) { $f = $i%2 ? 'test1' : 'test2'; $f(); // call function $f } ?> Quote Link to comment https://forums.phpfreaks.com/topic/291285-invoke-variable-method/#findComment-1492106 Share on other sites More sharing options...
AlexElkins Posted September 26, 2014 Author Share Posted September 26, 2014 (edited) do you mean like this? <?php function test1() { echo "Test 1<br>"; } function test2() { echo "Test 2<br>"; } for ($i=0; $i<4; $i++) { $f = $i%2 ? 'test1' : 'test2'; $f(); // call function $f } ?> No, because the possibilities aren't limited. It isn't feasible to make a function with the name of the output as it has no bounds. I'm starting to think that I would have to create a class for each variable to turn it into an object so that I could have the __invoke method with it as well. $account->phone_number = '123-456-7890'; and then I could also CALL phone_number and use what is passed to it $account->phone_number('LIKE %456%'); function phone_number($var) { return mysql_query('SELECT FROM table WHERE phone_number ' . $var); } Edited September 26, 2014 by AlexElkins Quote Link to comment https://forums.phpfreaks.com/topic/291285-invoke-variable-method/#findComment-1492108 Share on other sites More sharing options...
requinix Posted September 26, 2014 Share Posted September 26, 2014 1. Make phone_number private 2. Write __get/set to get and set that value 3. Write __call to provide the filtering or whatever support 1 Quote Link to comment https://forums.phpfreaks.com/topic/291285-invoke-variable-method/#findComment-1492114 Share on other sites More sharing options...
AlexElkins Posted September 26, 2014 Author Share Posted September 26, 2014 1. Make phone_number private 2. Write __get/set to get and set that value 3. Write __call to provide the filtering or whatever support Can the __call method detect which variable is DOING the calling? Quote Link to comment https://forums.phpfreaks.com/topic/291285-invoke-variable-method/#findComment-1492133 Share on other sites More sharing options...
Strider64 Posted September 26, 2014 Share Posted September 26, 2014 (edited) The call method would know what the attribute(variable) is doing because you would have to set it. Here's a little script that I put together using all magic methods (I personally would probably never use all magic methods, but that is just me): <?php class Phone { private $number = array(); public function __set($name, $value) { $this->number[$name] = $value; } public function __get($name) { if (array_key_exists($name, $this->number)) { return $this->number[$name]; } } public function __call($method, $arg) { if (array_key_exists($method, $this->number)) { return "Thank You for calling " . $this->number[$method] . "<br>"; } } } $myNumber = new Phone; $myNumber->phoneNumber = '555-421-4685'; // Set the attribute: echo $myNumber->phoneNumber; // Get the attribute: echo '<br>'; echo $myNumber->phoneNumber(); // Call the method: echo '<br>'; $myNumber->telephone = '555-555-1313'; // Set the attribute: echo $myNumber->telephone; // Get the attribute: echo '<br>'; echo $myNumber->telephone(); // Call the method: Edited September 26, 2014 by Strider64 1 Quote Link to comment https://forums.phpfreaks.com/topic/291285-invoke-variable-method/#findComment-1492138 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.