note 57570 deleted from language.oop5 by colder

From: Date: Sun, 19 Aug 2007 23:34:19 +0000
Subject: note 57570 deleted from language.oop5 by colder
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-129624@lists.php.net to get a copy of this message
Note Submitter: epeterson5 at student dot gsu dot edu ---- This isn't explicity noted anywhere, I don't think. If you use a variable after the "->" part of a class attribute call, it will parse the variable as a string to assign/compare that class attribute: <?php class User { var $test; var $test2; } $user1 = new User(); $user2 = new User(); $user1->test = "user1's test"; $user1->test2 = "user1's test2"; $user2->test = "user2's test"; $user2->test2 = "user2's test2"; print_r($user1); print_r($user2); foreach($user1 as $key => $value) $user2->$key = $value; print_r($user1); print_r($user2); ?> Which outputs: User Object: user1 [test] => user1's test [test2] => user1's test2 User Object: user2 [test] => user2's test [test2] => user2's test2 User Object: user1 [test] => user1's test [test2] => user1's test2 User Object: user2 [test] => user1's test [test2] => user1's test2 This is particularly useful when you want to assign a different instance of the same class to $this- I have a standard method that I call when I need to do that: <?php function assignToThis($new) { foreach($new as $key => $value) $this->$key=$value; } ?>

« previous php.notes (#129624) next »