Skip to content

Instantly share code, notes, and snippets.

@rajvanshipradeep15
Created August 7, 2017 06:15
Show Gist options
  • Save rajvanshipradeep15/70c7fc1f57fdf77136db282b628f9ec2 to your computer and use it in GitHub Desktop.
Save rajvanshipradeep15/70c7fc1f57fdf77136db282b628f9ec2 to your computer and use it in GitHub Desktop.
<?php
// your code goes here
class Example {
private $search;
public function __construct ($search) {
$this->search = $search;
}
public function setSearch ($search) {
$this->search = $search;
}
public function getReplacer ($replacement) {
echo "\n";
echo "im inside getReplacer: ". $replacement;
return function ($text) use ($replacement) {
echo "\n";
echo "text: " . $text;
echo "\n";
return str_replace ($this->search, $replacement, $text);
};
}
}
$example = new Example ('hello');
$replacer = $example->getReplacer ('goodbye');
//print_r( $replacer );
echo "\n";
echo $replacer ('hello world'); // goodbye world
$example->setSearch ('world');
echo "\n";
echo $replacer ('hello world'); // hello goodbye
$replacer = $example->getReplacer ('pradeep');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment