[PHP] dynamical class variable definition

2004-09-13 Thread Mario Lopez
Hi,

I have a class, but the problem is that
its variables need to be defined dynamically.

example:

class class_myclass{
 var $variable1;
 var $variable2;
 var $variable3;
...
 var $variableN


how to initialize these $variableN class variables from a global array which 
contains
the variable names and custom count of them?

if that's not possible.. maybe there's a possibility to initialize class 
variables on the fly
from constructor class

but the whole point is that the names and number of variables each php 
script run is
different

any suggestions?
thanks a lot! 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] dynamical class variable definition

2004-09-13 Thread Greg Donald
On Mon, 13 Sep 2004 18:26:56 +0300, Mario Lopez [EMAIL PROTECTED] wrote:
 Hi,
 
 I have a class, but the problem is that
 its variables need to be defined dynamically.
 
 example:
 
 class class_myclass{
  var $variable1;
  var $variable2;
  var $variable3;
 ...
  var $variableN
 
 how to initialize these $variableN class variables from a global array which
 contains
 the variable names and custom count of them?
 
 if that's not possible.. maybe there's a possibility to initialize class
 variables on the fly
 from constructor class
 
 but the whole point is that the names and number of variables each php
 script run is
 different
 
 any suggestions?


You can do array binding pretty easily:

function bindArray($array, $obj){
is_array($array) or die(bindArray() failed: array expected);
foreach($array as $k = $v){
$obj-$k = $array[$k];
}
}


-- 
Greg Donald
http://destiney.com/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] dynamical class variable definition

2004-09-13 Thread Mario Lopez
Oh Thanks,
I thought that only those variables that are defined with VAR
will be accessible and trieted as class variables

Mario 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] dynamical class variable definition

2004-09-13 Thread Greg Donald
On Mon, 13 Sep 2004 19:06:20 +0300, Mario Lopez [EMAIL PROTECTED] 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/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php