I am trying to hack an ACL into a Template without making the Template aware of the ACL object in the class definition. The following code generates an undefined method Template::isAllowed
Why is this? TIA!
class ACL {
protected $allowed = array('anything');
public function isAllowed($what){
if(in_array($what, $this->allowed))
return true;
return false;
}
}
class Template extends stdClass { }
$Template = new Template;
$ACL = new ACL;
$Template->isAllowed = function($what) use($ACL) { return $ACL->isAllowed($what); };
if($Template->isAllowed('anything'))
echo 1;
else
echo 2;
This:
actually tells PHP to call a method
Template::isAllowed(), which obviously doesn’t exist as given by your fatal error.You cannot treat
Template::isAllowed()as if it were a real method by assigning a closure to a property. However you can still call the closure that is assigned to the$Template->isAllowedproperty (which is an instance ofClosure). To do that, you need to either assign the property to a variable then call that:Or use
call_user_func():