PHP 8.1.18 Released!

Voting

: three plus six?
(Example: nine)

The Note You're Voting On

DB on music_ml at yahoo dot com dot ar
20 years ago
[EDIT by danbrown AT php DOT net: Combined user-corrected post with previous (incorrect) post.]

You can't refer to a class variable from an anonymous function inside a class method using $this.  Anonymous functions don't inherit the method scope.  You'll have to do this:

<?php
class AnyClass {

var
$classVar = 'some regular expression pattern';

function
classMethod() {

  
$_anonymFunc = create_function( '$arg1, $arg2', 'if ( eregi($arg2, $arg1) ) { return true; } else { return false; } ' );

  
$willWork = $_anonymFunc('some string', $classVar);

}

}
?>

<< Back to user notes page

To Top