Jump to content

Variables outside of classes


colbur87

Recommended Posts

Hey guys, I have searched for the answer here but can't find anything. Ironically this probably a basic question, but I have never had the need to come across this.... Background: My script is broken down into: getdata.php check_data_class.php I am using includes so for simplicity, treat this as all on one script. The output.php is a simple script that receives post data from a form and processes it. I have it set up to first check the data and make sure the data is correct; example(is_numeric, strlen). I have wrote a class to handle this procedure. I want the class to check the data and return an array if errors are present. Then if my errors array is !empty, it will loop through and print the errors. Question: How should I go about declaring the $errors = array(); Should I declare it outside of the class, or inside the class a public? Here is the class, as of now I tried using 'return' to write to the array but its not working when I try and loop though it. I know my 'foreach' and the rest of the code is right, I think this is a scope problem. Anyone know, thank you in advance.

<?phpinclude_once 'output.php'; class check_data {	public function check_input ($target,$id) 	{		$errors = array();		if(!is_numeric(($target))		{			$errors[] = 'The' . $id . 'field may only be digits';		}		if(strlen($target) > 2)		{			$errors[] = 'The' . $id . 'field must be only 2 digits in length'; 		}		return $errors;	}}?>

Link to comment
Share on other sites

It should be a property in the class. Depending on how you use that class, you may want to make it a static property.http://www.php.net/manual/en/language.oop5.properties.phphttp://www.php.net/manual/en/language.oop5.static.php
Thank you for your reply. If I declare the array as static and inside the class, am I able to call the array outside of the instance?
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...