Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>How about throwing exceptions? you can catch exceptions explicity with try /catch blocks, and/or catch them using <code>set_exception_handler()</code></p> <p>there are a number of useful exception types defined in PHP, which you can use to your advantage if you need granularity in exeption handling. plus you can define custom exceptions.</p> <p><a href="http://php.net/manual/en/function.set-exception-handler.php" rel="nofollow">http://php.net/manual/en/function.set-exception-handler.php</a> http://www.php.net/manual/en/spl.exceptions.php</p> <p><strong>EDIT</strong></p> <p>To answer your question about how some other frameworks approach this problem - judicious use of exceptions seems pretty common. The useful thing about using them is, say you have a particular method that does a number of different validations that might possibly be erroneous - you can throw an appopriate exception in each case, but you don't have to handle the different possible exceptions in that method. instead, depending on how you structure your code, you can allow the exception to bubble up to a more centralised place in your code where you can catch it and handle it appropriately.</p> <p><strong>EDIT 2</strong></p> <p>To elaborate on my last comment about <a href="http://php.net/manual/en/function.filter-input-array.php" rel="nofollow"><code>filter_input_array()</code></a></p> <p>Based on a really simple example with POSTed user data. First create a definition:</p> <pre><code>$userFormDefinition = array( 'email' =&gt; FILTER_VALIDATE_EMAIL, 'age' =&gt; FILTER_VALIDATE_INT, 'name' =&gt; array( 'filter' =&gt; FILTER_VALIDATE_REGEXP, 'options' =&gt; array('regexp' =&gt; '/^\w+$/') ), ); </code></pre> <p>Then using a generic validation class (class definition below):</p> <pre><code>$formValidator = new FormValidator(); $formValidator-&gt;validatePost($userFormDefinition); if ($formValidator-&gt;isValid()) { // if valid, retrieve the array // and use the values how you wish $values = $formValidator-&gt;getValues(); // for example, extract and populate // a User object, or whatever :) extract($values); $user = new User(); $user-&gt;setName($name); $user-&gt;setEmail($email); $user-&gt;setAge($age); // etc. } </code></pre> <p>A very basic (and untested) implementation of a FormValidator. </p> <p>The basic use case is to call the appropriate method for the request method to filter against. This in turn checks the returned values and decides if the input is valid.</p> <p>This could use a lot of love - <em>especially</em> the <code>filterInput</code> method, because you might have to do some testing to make sure you handle 'truthy' or 'falsy' values appropriately. I'm thinking checkbox type values. A straight up <code>in_array</code> check for <code>false</code> might not cut it as implemented here. But there are loads of flags that you can pass in with the definition.</p> <p>I guess you could also check for missing inputs by comapring a count of the resulting <code>$values</code> array and the definition, to make sure they match. Additional inputs not in the definition are filtered out (you might want to check that but I'm reasonably sure about this off the top of my head).</p> <pre><code>&lt;?php class FormValidator { private $isValid = false; private $isBound = false; private $values = array(); public function validatePost(array $definition) { // additional REQUEST_METHOD checking here? $this-&gt;filter(INPUT_POST, $definition); } public function validateGet(array $definition) { // additional REQUEST_METHOD checking here? $this-&gt;filterInput(INPUT_GET, $definition); } protected function filterInput($type, $definition) { $this-&gt;isBound = true; $this-&gt;values = filter_input_array($type, $definition); // might have to do some reading on nulls vs false, // and validating checkbox type values here... you can // set all sorts of flags so a better implementation // would probably be required here... :s if (is_array($this-&gt;values) &amp;&amp; !in_array(false, $this-&gt;values))) { $this-&gt;isValid = true; } } public function isValid() { if (!$this-&gt;isBound) { throw new Exception("you didn't validate yet!"); } return $this-&gt;isValid; } public function getValues() { if (!$this-&gt;isBound) { throw new Exception("You didn't validate yet!"); } return $this-&gt;values; } } </code></pre> <p>Anyway, I would say refactor and test the bejayzis out of that class, (or even totally change it) but hopefully it outlines the basic idea: for each type of input, create a definition and then use a generic validation class to filter and ensure validity.</p> <p>Hope this helps. <code>filter_input</code> and <code>filter_input_array</code> rock :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload