Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'll try to explain what happens briefly, so I'll use terms which may be not fully correct from the technical point of view. </p> <p>In your code, two <em>inner</em> functions <code>generateCharacter</code> and <code>generateNumber</code> are defined <em>in the body</em> of the <em>outer</em> function <code>generateString</code>. </p> <p>Initially, only the outer function (<code>generateString</code>) is "available" for the rest of your code. </p> <p>When the outer function gets called, it "defines" the two inner functions, and these become available in the global scope. I.e. the inner functions are <em>not really private</em> - they are exported into the <em>global</em>, not even class scope! </p> <p>When you call the outer function <code>generateString</code> more than once, the inner functions are getting "defined" more than once - as if you would define them more than once manually in the <em>global</em> scope. This is not allowed in PHP, since the error. </p> <hr> <p>Possible fixes: </p> <ul> <li><p>Move inner functions into the global scope (they are exported there on the first call anyway). This it the worst way to go. </p></li> <li><p>Make <code>generateCharacter</code> and <code>generateNumber</code> simple private methods. </p></li> <li><p>Use PHP's <a href="http://php.net/uniqid" rel="nofollow"><code>uniqid</code></a> as suggested in the other answer. </p></li> </ul> <hr> <p>Here a simplified scenario, to see what happens: </p> <pre><code>class Test { public function aaa() { function bbb() { echo("bbb"); } echo "aaa"; bbb(); } } // bbb(); // would produce fatal error: undefined function bbb() $test = new Test(); $test-&gt;aaa(); // outputs "aaabbb" bbb(); // ok here, outputs "bbb" </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. This table or related slice is empty.
    1. This table or related slice is empty.
    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