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

}

Edited by AlexElkins
Link to comment
Share on other sites

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 by Strider64
  • Like 1
Link to comment
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.