I have a class I am coding that will validate forms entered by users.

I have set up my class, but now I have a strange little problem i cannot explain or fix....

I keep getting an error that array_push must have an array as a parameter - but I do. I thought it had something to do with me using $this->arrayName as the argument in the function - array_push($this->arrayName,$value);

So I changed it to -

$arr = $this->arrayName;

array_push($arr,$value);

$this->arrayName = $arr;

It still complained that I need an array as an argument.

So I thought it was the way I was declaring the variables when the class is initialized BUT-

Instance variables are declared in the class like so -

class formValidator
{
	var $fields; 
	var $values;
	var $patterns;
	var $errors;

...rest of class

And the constructor looks like this -

function __construct()
{
	$fields = array();
	$values = array();
	$patterns = array();
	$errors = array();

...rest of constructor stuff - too long to list here, but does absolutely nothing to the fields array

So far, the function has now evolved into this -

function add_field($longName,$fieldName,$required,$fieldType,$validationType,$size,$specialPattern)
{

$fieldArray = array("longName"=>"$longName","fieldName"=>"$fieldName","required"=>"$required","fieldType"=>"$fieldType","validationType"=>"$validationType","size"=>"$size","specialPattern"=>"$specialPattern");

	array_push($this->fields,$fieldArray);
	//$arr = $this->fields;
                  //array_push($arr,$fieldArray);
                  //$this->fields = $arr;

	RETURN 1;

}//end add_field

I am showing my commented out code in case you want to see the history of what I have attempted.

What am I missing here?

-Tony

    Try this:

    function __construct() 
    { 
        $this->fields = array(); 
        $this->values = array(); 
        $this->patterns = array(); 
        $this->errors = array();
        // other code
    }

    or:

    var $fields = array(); 
    var $values = array(); 
    var $patterns = array(); 
    var $errors = array();
    
    function __construct() 
    { 
        // other code
    } 
    

    The manual says about array_push():

    Note: If you use array_push() to add one element to the array it's better to use $array[] = because in that way there is no overhead of calling a function.

      hmmm - I was not aware of the note on array_push(), and it makes sense now that you think about it.

      Thanks for the reply!

      I will try it first thing in the morning.

        Write a Reply...