colbur87 Posted August 10, 2011 Share Posted August 10, 2011 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 More sharing options...
justsomeguy Posted August 10, 2011 Share Posted August 10, 2011 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 Link to comment Share on other sites More sharing options...
colbur87 Posted August 10, 2011 Author Share Posted August 10, 2011 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 More sharing options...
colbur87 Posted August 11, 2011 Author Share Posted August 11, 2011 Can anyone help me out on this? Link to comment Share on other sites More sharing options...
thescientist Posted August 11, 2011 Share Posted August 11, 2011 http://php.net/manual/en/language.oop5.static.php Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.