note 69126 added to function.get-object-vars
| From: | armantic101 at gmail dot com | Date: | Thu, 24 Aug 2006 02:01:25 +0000 |
| Subject: | note 69126 added to function.get-object-vars | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-116245@lists.php.net to get a copy of this message | ||
I've been spoiled by OOP in C++ so I thought it was strange there was no built in copy
constructor (At least not one that I could find ..sorry, I'm still kinda new to PHP).
I was able to use get_object_vars to simulate this. Here it is if anyone else can find it usefull.
<?php
class SomeObj {
private $myVar1 = 0;
private $myVar2 = 0;
# constructor
function SomeObj($_obj = Null) {
#check just to make sure ;-)
if (get_class($_obj) == get_class($this) ) {
$arMyVars = get_object_vars($_obj);
foreach ($arMyVars as $name => $value) {
$this->$name = $value;
}
}
}
function changeVars($_var1, $_var2) {
$this->myVar1 = $_var1;
$this->myVar2 = $_var2;
}
function printVars() {
print "$this->myVar1 $this->myVar2\n";
}
}
$oldObj = new SomeObj();
$oldObj->printVars();
$oldObj->changeVars(1, 2);
$newObj = new SomeObj($oldObj);
$newObj->printVars();
?>
output:
0 0
1 2
Hope someone can find this usefull.
...now to figure out how to make multiple constrictors.
----
Server IP: 64.71.164.2
Probable Submitter: 24.22.178.144
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.get-object-vars.php
Edit -- https://master.php.net/note/edit/69126
Del: integrated -- https://master.php.net/note/delete/69126/integrated
Del: useless -- https://master.php.net/note/delete/69126/useless
Del: bad code -- https://master.php.net/note/delete/69126/bad+code
Del: spam -- https://master.php.net/note/delete/69126/spam
Del: non-english -- https://master.php.net/note/delete/69126/non-english
Del: in docs -- https://master.php.net/note/delete/69126/in+docs
Del: other reasons-- https://master.php.net/note/delete/69126
Reject -- https://master.php.net/note/reject/69126
Search -- https://master.php.net/manage/user-notes.php