Jump to content

get_object_vars using a reference?


davidmyers

Recommended Posts

In the past for my getter and setter functions I've always just used a switch statement with a case for each class variable. However, I decided to re-write them to be more dynamic because those switch statements can get really really long and also I would have to change them anytime I made a change to the class variables, ie adding or removing any. So this is what I came up with for the getter function:

 

function GetValue($Variable)
{
$ObjVars = get_object_vars($this); // Grab associative array of Class Variables
$Variable = strtolower($Variable); // Convert argument of variable name to lowercase
if(array_key_exists($Variable, $ObjVars)) // Check to see if argument is valid class variable
{
	return $ObjVars[$Variable]; // Return value of requested variable
}
else
{
	$Error = new Error("You must supply a valid variable to User->GetValue()", $_SESSION['CurrentUser']->GetValue(ID), 'User', $_SESSION['CurrentPage'], 'PHP', 'Medium');
	$Error->Log();
	$Error->Display();
	return "error";
}
}

 

Obviously though this solution doesn't exactly work for a setter function since changing the value of a variable in $ObjVars won't have any effect on the actual class/object variable. The idea I had was if I could pass $this as a reference to get_object_vars then it would give me references to the variables and by changing the values in $ObjVars I would in change the actual class/object variables. So I tried:

 

$ObjVars = get_object_vars(&$this);

 

But got this error: "Call-time pass-by-reference has been deprecated"

Upon researching this I realized it was because you don't pass references in this way in PHP. As far as I can tell, unless a function is written to return it's value as a reference, there really isn't a way to make it do so. So short of writing my own function to emulate get_object_vars and have it return references, is there another way to solve this?

 

I feel like if this can't be done how I'm currently trying, that means there's a better way to do it. I just haven't figured it out yet. So any and all help or suggestions are greatly appreciated.

Link to comment
https://forums.phpfreaks.com/topic/241471-get_object_vars-using-a-reference/
Share on other sites

works:

$ObjVars = &get_object_vars($this);

That syntax is correct, but that doesn't work because get_object_vars is not written to return references.

 

Your solution won't really work for me because it isn't dynamic in the way that I'm wanting.

 

I'm trying to reference class/object variables without explicitly naming them anywhere except in the argument to the function if that makes sense.

 

Also, the GetValue() function I posted works perfectly, it's the SetValue() version of the same code I'm trying to get working.

Call-time pass-by-reference has been deprecated - Solution

Since this is fortunately localised to a single function, it turned out to be easy to remove the function and replace it with code that modified the data object directly.

 

I appreciate you trying to help, but I have no idea what you're talking about and that doesn't answer anything.

ERROR:

Warning: Call-time pass-by-reference has been deprecated - argument passed by value;

Solution 1:

 

Open the ini.inc.php file and add the following line after the open php tag

ini_set("allow_call_time_pass_reference",true);

Solution 2:

 

Create an .htaccess file in the root folder in your server with the content below:

 

php_flag allow_call_time_pass_reference on

 

If you already have an .htaccess in the root file then just added this to the top.

Ok, so I tried the .htaccess option first since it's the easiest but it didn't do anything.

 

I'd try the first solution but I don't know where that file is.

 

Ideally I'd like a solution that doesn't involve changing settings in PHP that have been deprecated for a reason. I realize that the way I initially tried to get this to work was the wrong way, but I can't think of any other way and that's really where I'd like help.

Ok, so I found a solution to my problem.

 

Here is the code that I've ended up with:

function SetValue() // Used to set values in current object
{
if(func_num_args() != 0) // Check for arguments
{
	$Arguments = func_get_args(); // Get arguments
	$Variable = $Arguments[0]; // Set variable
	if(is_array($Variable)) // Check if first argument is an array of multiple variables to be set
	{
		$ObjVars = get_object_vars($this); // Grab associative array of Class variables
		foreach($Variable as $key => $value) // Loop through argument array
		{
			if(array_key_exists($key, $ObjVars)) // Check if argument array key is valid class variable
			{
				$this->$key = $value; // Set object variable with new value
			}
			else
			{
				// Send error
				return "error";
			}
		}
	}
	else // Argument isn't an array so we're only setting one variable
	{
		$Value = $Arguments[1]; // Set variable
		$ObjVars = get_object_vars($this); // Grab associative array of Class variables
		$Variable = strtolower($Variable); // Convert argument of variable name to lowercase
		if(array_key_exists($Variable, $ObjVars)) // Check to see if argument is valid class variable
		{
			$ObjVars[$Variable] = $Value; // Update array with new variable value
			foreach($ObjVars as $key => $value) // Loop through array of variables
			{
				$this->$key = $value; // Set object variable with new value
			}	
		}
		else // Not a valid class variable
		{
			// Send error
			return "error";
		}
	}
}
else
{
	// Send error
	return "error";
}
}

 

I know this topic was up for less than 24 hours, but it seems like every time I post a question I get almost no responses and no one ever gives me a solution. Either I'm not doing something right when I ask for help, or I guess there isn't much help to be had for more advanced issues? It's kind of frustrating because instead of getting the help I'm seeking I end up just getting answers that are irrelevant and solving the problem myself eventually.

 

Sorry for the rant. In any event, here is the code for a completely dynamic Class Setter function that will either accept a variable and value or an associative array of variables and values.

 

EDIT: I forgot two $'s in the code above.

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.