Re: assigning objects
| From: | Ignacio Vazquez-Abrams | Date: | Tue, 14 Nov 2000 23:38:58 +0000 |
| Subject: | Re: assigning objects | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-25290@lists.php.net to get a copy of this message | ||
On Tue, 14 Nov 2000, David P. Schwartz wrote:
> If I have a class that is used to represent a row in a db-table, defined thusly:
>
> class mytable
> {
> var $fld1, $fld2, $fld3;
> :
> :
> function select_db( $where_str )
> {
> :
> $result = mysql_query( blah-blah );
> if ($result)
> $obj = mysql_fetch_object( $result );
> $this = $obj; // does not work!
> }
> }
>
> Is there any way to assign the $obj values to corresponding $this vars, other
> than saying:
>
> $this->fld1 = $obj->fld1;
> $this->fld2 = $obj->fld2;
> $this->fld3 = $obj->fld3;
>
> In c++ this would be accomplished with a copy constructor which, for most simple
> cases like this, is implicitly defined.
>
> Thanks
> -David
>
<?php
$a=get_object_vars($obj) // or just use mysql_fetch_array()
while (list($key, $val)=each($a))
$this->$key=$val;
?>
Good enough?
--
Ignacio Vazquez-Abrams <ignacio@openservices.net>