depojones Posted April 9, 2009 Share Posted April 9, 2009 Hi genius, I am processing a form on the same page using the procedural approach like this: <?php if(isset($_POST['submit'])) { if(trim($_POST['firstname']=="")) { echo 'firstname is missing'; } else{ echo 'firstname is fine'; } } ?> How do I apply such logic using OOP approach. Thanks in advance Link to comment https://forums.phpfreaks.com/topic/153345-solved-trying-to-achieve-this-in-oo/ Share on other sites More sharing options...
JonnoTheDev Posted April 9, 2009 Share Posted April 9, 2009 This is not a case for OOP. However you could create a validation object. You would have to assign the input and the test you wish to run. i.e. class validate { private $data; public $errors; public function __construct($data) { $this->data = $data; $this->errors = array(); $this->processData(); } private function processData() { // loop through data and run each test } private function testNumeric() { } private function testEmpty() { } } $data = array(array($_POST['firstname'],'empty'), array($_POST['age'],'numeric')); $validation = new validate($data); if(count($validation->errors)) { // display errors } Link to comment https://forums.phpfreaks.com/topic/153345-solved-trying-to-achieve-this-in-oo/#findComment-805653 Share on other sites More sharing options...
Axeia Posted April 9, 2009 Share Posted April 9, 2009 Bit too small of an example to make a class out? <?php class Mon() { /** * @param String $str * @return String */ function missingOrNot( $str ) { if( trim( $str ) == '' ) { return $str.' is missing'; } else { return $str.' is fine'; } } } if(isset($_POST['submit'])) { $monInstance = new Mon(); echo $Mon->missingOrNot( $_POST['firstname'] ); } ?> Link to comment https://forums.phpfreaks.com/topic/153345-solved-trying-to-achieve-this-in-oo/#findComment-805654 Share on other sites More sharing options...
depojones Posted April 9, 2009 Author Share Posted April 9, 2009 @ All The example was just an understanding of what am trying to do. I have a form of over 30 input and I will be testing all for errors. Lastly, can I include my class file at the top of the form and if Yes, how is the whole process like. Am new to PHP, please bear with me Link to comment https://forums.phpfreaks.com/topic/153345-solved-trying-to-achieve-this-in-oo/#findComment-805664 Share on other sites More sharing options...
depojones Posted April 9, 2009 Author Share Posted April 9, 2009 I dont know if I need to provide more clue. I can perform this task comfortably using the procedural approach. I still don't get the OO method. Link to comment https://forums.phpfreaks.com/topic/153345-solved-trying-to-achieve-this-in-oo/#findComment-805681 Share on other sites More sharing options...
Maq Posted April 9, 2009 Share Posted April 9, 2009 Then you should read in the manual for examples and explanations of how to code in OOP: PHP OOP Link to comment https://forums.phpfreaks.com/topic/153345-solved-trying-to-achieve-this-in-oo/#findComment-805687 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.