Merging object variables into another class/object.
| From: | Paul McGarry | Date: | Thu, 11 Oct 2001 00:33:41 +0000 |
| Subject: | Merging object variables into another class/object. | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-70748@lists.php.net to get a copy of this message | ||
Is there a nice way of merging variables belonging to an object into variables
belonging to a class?
For example say I have a class like:
====
class Entry
{
var $entry_id;
var $name;
var $description_html;
function Entry ($ent_id)
{
global $conn;
$sql= 'SELECT * FROM entry where entry_id='.addslashes($ent_id)." AND
live='t'";
$result=pg_exec($conn,$sql);
$tmp=pg_fetch_object($result,0);
$this->entry_id=$tmp->entry_id;
$this->name=$tmp->name;
$this->description_html=$tmp->description_html;
}
====
having to do all the $this->x=$tmp->x is annoying (in my real example there
are considerably more columns).
I'd prefer something like:
====
class Entry
{
var $entry_id;
var $name;
var $description_html;
function Entry ($ent_id)
{
global $conn;
$sql= 'SELECT * FROM entry where entry_id='.addslashes($ent_id)." AND
live='t'";
$result=pg_exec($conn,$sql);
oo_merge($this,pg_fetch_object($result,0));
}
====
I guess it's possible to write an oo_merge function in PHP using get_class_vars() and
get_object_vars() as appropriate but I was wondering whether this would be generically
useful and therefore worth having in the core language?
Paul