Please take a look at this piece of code. My question is in the constructor, inside the foreach. Am I creating variables this way?
For example, is the array is like:
$form_args_array[type1]= value1;
$form_args_array[type2]= value2;
$form_args_array[type3]= value3;

then I will have this variables??? ok??

$this->type1= value1
$this->type2= value2
$this->type3= value3

But , If for my second instance I change the input array, I`ll have two instances of the same class with completely different variables. Is this posible?

or the values of the array MUST contain one of the predefined variables defined earlier in the class?

class StdForm {
var $name; // Nombre del formulario
var $fields_array; // Array de campos
var $FORMPATH; // Path donde están los archivos de este form
var $std_evaluate; // ¿Evaluar campos obligatorios y pictures?

#-> Constructor
function StdForm($form_args_array)
{
$this->name = $form_args_array[form_name]; //Por compatibilidad
foreach($form_args_array as $arg => $value) {
$this->$arg = $value;
}

Thanks for your help.

    Ok, from what I see, you're tring to dynamically create instance variables in your class from an array of form variables.

    Try thinking the other way around. Store a reference to the array in your class and write accessor methods to it. For instance (no punn intended):

    class StdForm {
    	var $name;
    	var $fields_array;
    
    // Constructor
    function StdForm(&$form_args_array)
    {
    	$this->name = $form_args_array['form_name'];
    	$this->fields_array = $form_args_array;
    }
    
    function getFormVar($var_name)
    {
    	return $this->fields_array[$var_name];
    }
    }
    
    $form = &new StdForm($_GET);
    print $form->getFormVar("form_var_name");
    

    And then you dont have to worry about creating an instance variable for every form var, just leave them where they are and access them by reference any that will also save you alot of copying large amounts for from vars. By adding the & to the paramter for your constructor you force the array to be passed in by reference. So your STDForm will not hold the actual array but just a refernce to it.

      Thank you for your answer.
      I think I understand your point. But it is another persons code and I'm just trying to understand it.

      You said: "from what I see, you're tring to dynamically create instance variables in your class from an array of form variables. "

      Thats what I thought. So my questions are (to put it clear)

      Is it posible to dinamically create instance variables in a class in some way?

      Cause it sounds a little weird to me that 2 instances of the same class could have a different set of variables.

        Yes it is possible to dynamically create attributes in a class. And yes this means that two objects of the same class can have different variables. And yes it is a bit unusual (for oop languages) but it works well in practice. In this context, objects and assoc arrays are basically equivalent.

        Consider:

        $user = mysql_fetch_array($result);
        echo $user['fname'];
        
        $user = mysql_fetch_object($result);
        echo $user->fname;
        

        mysql_fetch_object always returns the same class of object but it's variables will depend on the query results.

        By the way, in your code use:
        $this->{$arg} = $value;

          Write a Reply...