how to create Auto Setter method in PHP?

Hello all,

 I would like to create auto setters. What I would like to do is pass an object to class method and it will set all class variables to passed object values (if class variable exits)

Something like;
public static function setter ($object=NULL) {
		if (is_object($object)) {
			foreach ($object as $key => $value) {
				if (property_exists($this, $key)) {
						$$this->$key = $value;
					}				
			}
			return $this;
		}

Open in new window


 But this code doesn't work. When I try to echo  just shows a blank page because it is not set.
echo $class->class_var;

Open in new window


 How could I solve this problem? Thank you for your help and concern in advance.
LVL 2
pixalaxAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mydropzCommented:
if (property_exists($this, $key)) {
						$$this->$key = $value;
					}

Open in new window

why do you use double dolar sign for this?

i think that creates the problem because double dollar sign doesn't set the value but treats the value as the name of the variable so $this will stay empty
pixalaxAuthor Commented:
If I don't it gives error saying that I can't use $this
Ray PaseurCommented:
$this is a reserved word.
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

Ray PaseurCommented:
Can you explain why you would want to do this?  It would seem to me that injecting variables into your namespace somewhat defeats the value of encapsulating your data in object-oriented code.  A better design pattern would be one that passed an array or something like that, if you need to pass a lot of variables at once.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mydropzCommented:
use something else rhan $this maybe you could use $that add it to te beginning of the function like
$that='';

Open in new window

pixalaxAuthor Commented:
Thank you, instead of going to deal and fight over class variables, I just made an array of them and assigned values to them.

This is making coding lines a bit longer but it is a solution in the end.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.