Last active
December 10, 2015 16:59
-
-
Save awreece/4464737 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ php test.php | |
Fatal error: Call to a member function get_string() on a non-object in /private/tmp/test.php on line 11 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Foo { | |
function get_string() { | |
return "world"; | |
} | |
function parse_string($string) { | |
$y = $this; | |
$callback = function ($match) use ($x, &$y) { | |
return $x->get_string(); | |
}; | |
return preg_replace_callback("/{{name}}/", $callback, $string); | |
} | |
} | |
$x = new Foo(); | |
echo $x->parse_string("hello {{name}}") . "\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment