Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to call a function stored as string from inside a class
    primarykey
    data
    text
    <p><strong>EDIT: Sorry guys, it was a typo =(</strong></p> <p>I feel embarrassed for wasting your time on this. I'm leaving this thread open in hopes that someone might find the discussed information useful.</p> <p><em>To clarify further, the code below will work as expected, I mistyped 'classHandler' in my code, and that's the reason PHP couldn't find it.</em></p> <p><em>Also, the syntax errors noted by some commenters have been corrected</em></p> <p>I feel obligated to summarize the discussion on this thread:</p> <hr> <p>@wimvds suggests using inheritance and that my implementation is not a good practice.</p> <p>@Victor Nicollet disagrees saying extending the behavior of an existing instance is 1. impossible and 2. bad design</p> <p>@ircmaxell adds: Remember, you should always favor composition over inheritance. And this isn't spaghetti code, it's a pretty standard method of implementing a stripped down notification system (You could build a full blown observer or mediator pattern, but for really simple tasks, this may be a whole lot better since it's easier to maintain).</p> <p>@Victor asks for a minimal one file example returning this error. This is what helped me solve the issue. When I tried the sample file, it work perfectly leading me to believe that something else indeed was wrong.</p> <p>@Shakti Singh suggests trying <code>call_user_func( array( $this, $this-&gt;handler ), $var);</code></p> <p>@Victor Nicollet responds saying This would attempt to call a member function $this->classHandler (which probably doesn't exist) instead of the global classHandler</p> <p>@abesto gives it a shot, ends up with a very similar implementation as my own, which works without my typo.</p> <p>@Victor Nicollet answers by claiming that the classHandler needs to be defined prior to the call.</p> <p>@Blizz responds by saying that PHP parses classes and functions first and then the regular code.</p> <hr> <p><strong>MyClass.php</strong> ( singleton )</p> <pre><code>public $handler; public function myMethod() { $var = "test"; call_user_func( $this-&gt;handler, $var ); // PHP Warning: First argument is expected to be a valid callback } </code></pre> <p><strong>Script.php</strong></p> <pre><code>$myClass = new MyClass; $myClass-&gt;handler = "classHandler"; $myClass-&gt;myMethod(); function classHandler( $var ) { echo $var; } </code></pre> <p>If this is incorrect, what is the commonly practiced means of invoking handlers / event handlers in php?</p> <p><em>Note that this is a simplified version of the actual script</em></p>
    singulars
    1. This table or related slice is empty.
    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.
 

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