Skip to content

Instantly share code, notes, and snippets.

@awreece
Last active December 10, 2015 16:59
Show Gist options
  • Save awreece/4464737 to your computer and use it in GitHub Desktop.
Save awreece/4464737 to your computer and use it in GitHub Desktop.
$ php test.php
Fatal error: Call to a member function get_string() on a non-object in /private/tmp/test.php on line 11
<?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