Jump to content

Invoke Variable Method?


AlexElkins

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/291285-invoke-variable-method/
Share on other sites

 

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);

}

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:

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.