Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In PHP by default all values to functions are passed as values, not references (unless they are objects).</p> <p>First of all, if you want to use a variable as a return value, you'd have to define <code>checkLength($var)</code> as:</p> <pre><code>function checkLength(&amp;$fields_with_lengths) { /* ... */ } </code></pre> <p>You could in PHP4 do a call like <code>checkLength(&amp;$errors)</code>, but that's deprecated as of PHP5 and will throw an <code>E_STRICT</code> warning. Of course, you still can (but that functionality may be thrown away soon), though the "correct" way is, as said, giving it as reference.</p> <p>An even more elegant way of acting, and what should really be done in PHP, is to return the <code>$fields_with_lengths</code>. This way:</p> <pre><code>function checkLength() { $var = array(); // Your code return $var; } </code></pre> <p>And in the call to the function, just say</p> <pre><code>$return = $validator-&gt;checkLength(); </code></pre> <p>But that does not solve the <em>logic</em> of your problem.</p> <p>You are iterating over a parameter which is an empty array. You are not iterating over $validator->fields_with_lengths. Even more, $fields_with_lengths, in your code is <em>not initialized ever</em>.</p> <p>You would have to call <code>$validator-&gt;setLengths()</code>. A better way of doing is (you are using PHP5) declaring a constructor, which in PHP5 is done with the magic function <code>__construct()</code>.</p> <p>However, as you are just initializing the variable always with the same <em>constant</em> values, you can do that in the very definition, just like this:</p> <p>So you would have something like:</p> <pre><code>class FormValidator { pubilc $first; pubilc $last; pubilc $email; pubilc $fields_with_lengths = array('first' =&gt; 2, 'last' =&gt; 2, 'email' =&gt; 8); // Rest of code } </code></pre> <p>What's more.. ¿Why are the attributes marked as public?</p> <p>OOP theory tells us to close the scope of visibility as much as possible. And as you have setters<code>, you don't need to access the attributes directly. You can in PHP have a magic</code>__get($var)<code>to return a private $attribute if you want the syntactic sugar of</code>echo $validator->first;`.</p> <p>Example:</p> <pre><code>function __get($var) { if(isset($this-&gt;{$var})) return $this-&gt;{$var}; // this $this-&gt;{$var} could even be written as $this-&gt;$var but I did it like that for clarity. } </code></pre> <p>If you also want the syntactic sugar of <code>$validator-&gt;first = "John";</code> you can do the same with the setter, as in:</p> <pre><code>function __set($var, $value) { if(isset($this-&gt;{$var})) { return $this-&gt;{$var} = $value; } return null; // Or you can throw an exception if you like } </code></pre> <p>In PHP, as opposed to other languages, such as Java, you must explicitly use $this as the current instance of an object within a function.</p> <p>So your <code>checkLength</code> is misdefined.</p> <p>You should also do something with the return value, instead of just dropping it.</p> <p>With just that minor modifications, you could fix your code with</p> <pre><code>function checkLength() { $length_errors = array(); foreach($this-&gt;fields_with_lengths as $fieldname =&gt; $maxlength) { if (strlen(trim($_POST[$fieldname])) &gt; $maxlength) { $length_errors[] = $fieldname; } } if (!empty($length_errors)) { $message = 'There were no errors'; } else { $message = 'There were errors'; } return array($length_errors, $message); } </code></pre> <p>You were using a variable ($length_errors otherwise inaccessible! so you were computing data which would never be useful).</p> <p>And in the call to the function, something like $validator = new FormValidator();</p> <pre><code>// You could leave the setXXX($_POST[xxx]) $validator-&gt;first = $_POST['first']; $validator-&gt;last = $_POST['last']; $validator-&gt;email = ($_POST['email']; $errors = $validator-&gt;checkLength(); echo "&lt;p&gt; {$errors[1]} &lt;/p&gt;"; if(!empty($errors[0])) { // You could do something like foreach($errors[0] as $k =&gt; $fieldname) { echo "Error in $fieldname&lt;br /&gt;" } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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