Re: dynamical class variable definition

From: Date: Mon, 13 Sep 2004 16:28:48 +0000
Subject: Re: dynamical class variable definition
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-196522@lists.php.net to get a copy of this message
On Mon, 13 Sep 2004 19:06:20 +0300, Mario Lopez <proximus@e-teliamtc.lv> wrote: > Oh Thanks, > I thought that only those variables that are defined with VAR > will be accessible and trieted as class variables Nope, it'll work: #!/usr/bin/php <?php function bindArray($array, &$obj){ is_array($array) or die("bindArray failed: array expected"); foreach($array as $k => $v){ $obj->$k = $array[$k]; } } class Obj { var $x = 1; function Obj(){} } $o = new Obj(); $a = array(1, 2, 3); bindArray($a, $o); print_r($o); ?> Produces: obj Object ( [x] => 1 [0] => 1 [1] => 2 [2] => 3 ) You may want to prefix your key names with some text however. :) -- Greg Donald http://destiney.com/

« previous php.general (#196522) next »