I have the following class:
class Decode {
public $code;
public $codeStore;
public $store;
public $varName;
public $storeNew = array();
public $storeNew = array();
public function __construct($code) {
$this->code = $code;
$this->codeStore = $code;
$this->varName = substr($this->code, 0, 30);
$this->varName = str_replace("var ", "", $this->varName);
$this->varName = substr($this->varName, 0, strpos($this->varName, "=[\""));
}
public function chrToVar() {
// The line below is line 38
$this->code = preg_replace_callback('/'.$this->varName.'\[([0-9]+)\]/', function($matches) { return $this->storeNew[$matches[1]]; }, $this->code);
}
}
$script = new Decode('stuff');
$script->chrToVar();
When I run this code, I get the following error:
Fatal error: Using $this when not in object context in /var/www/programs/decode.php on line 38
Why is this happening? I suppose it has something to do with the parameter that has the function in preg_replace_callback
, but I have no idea how to fix it.