Whats the use of procedural checking when there is OOP? Well lets say I wanted to check if a car’s door is closed and the light in the car only goes off when the door closes. In addition to that, if the light is off, the headlights also switch off. How can I have such procedures in OOP without returning critical errors? The answer is procedural checking!
Now after you read that first paragraph you probably realized you could do a series of if() {...}else{...} statements but if you like to keep things organized, the tabs will extend out too far making it annoying to check/improve coding. In addition to that it just seems primitive to use such a style (that’s if there’s not a method better than this one).
if() {...}else{...} Example
1 2 3 4 5 6 7 8 9 | <?php if($a == $b) { if($k == $c) { if($t == $x) { return 'Login Successful'; } } } ?> |
The procedural checking method is only useful when your checking over 2 or 3 functions or inputs. Anything less than that amount and this style of coding might be a waste of time since there isn’t that much checking. This style is only good for large amounts of checks. Note: Between if statements and this, results are the same. Just different styles (though procedural method might be more flexible).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <?php class login { // last checking number protected $_lastint = 0; // username obj protected $_user = NULL; // password obj protected $_pass = NULL; // error variable protected $_error = NULL; public function __construct($user='oh', $pass=md5('k')) { $this->_user = $user; $this->_pass = $pass; // run linear function // in this case there are 3 checks for($i=0;++$i;$i<=3) { // stops further processing if($this->checklist($i) === false) { break; } } } |
Everything is pretty ordinary in a normal class; we have initialized variables and a constructor. But the constructor also has a for loop that continuously calls the same function with a int parameter. That function returns true or false (Boolean).
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | // begin Procedural Checking public function checklist() { // its a critical error if the order is not in line if($int != ($this->_lastint+1)) { throw new Exception("Sequence is out of order"); } else { $this->_lastint = $int; } // count case number $i = 0; switch($int) { // some functions, all of them are booleans case ++$i: return $this->check_empty(); break; case ++$i: return $this->userpass_mysql(); break; case ++$i: return $this->check_captcha(); break; case ++$i: return $this->no_multiple_logins(); break; // its int doesnt exist, critical error out default: throw new Exception("Checklist default error"); break; } } |
If you implied that all the functions in checklist() are Boolean, you are correct. Each function is basically a single if() statement with the access of doing much more that what goes outside an if statement, without creating additional functions (unless needed for a complex work of art). But there is still a few things missing. The main question now should be: “How do I know which function stopped the loop?”
45 46 47 48 49 50 51 52 53 | protected function check_empty() { if($this->_user == '' || $this->_pass == '') { // outputs string $this->_error = 'error message goes here'; return false; } else { return true; } } |
We also need an error function so it can be checked if there are is an error; if there is return it.
54 55 56 57 58 59 60 61 62 63 | // if errors public function error_out() { if($this->_error != '') { // outputs string return $this->_error; } else { return false; } } } |
That’s about it. In conclusion, there’s an area where checklist() is called multiple times (linear format) which can stop if returned false, functions within checklist() which also returns true or false (using switch() statement), independent functions return true/false with the option to do individual tasks, and an error console. How to use that error console…
1 2 3 4 5 6 | // ....some coding here if($login->error_out() !== false) { // redirect or do whatever } else { echo $login->error_out() } |
Personally, I think this would be perfect for long forms like a registration area for a site.
Hope you guys like my concept, please comment back on questions or concerns you have.