Reputation: 4171
Evaluating a PHP library I found the following code, which seemed to me to be redundant, it makes me think
Why would you want a class to have callable property?
class OpenIDConnectClient
{
...
/**
* @var callable validator function for issuer claim
*/
private $issuerValidator;
...
public function __construct($provider_url = null, $client_id = null, $client_secret = null, $issuer = null) {
...
$this->issuerValidator = function($iss){
return ($iss === $this->getIssuer() || $iss === $this->getWellKnownIssuer() || $iss === $this->getWellKnownIssuer(true));
};
}
...
}
In the above example the callable property will be the same every time, so it is not about being dynamic, there seems no need to instantiate this in the constructor either, and why not just make it a method of the class?
Addendum Why on PHP Type declarations docs does it say this (emphasis mine):
callable - The value must be a valid callable. Cannot be used as a class property type declaration.
Upvotes: 3
Views: 1265
Reputation: 12939
On line 1318 you can see:
public function setIssuerValidator($issuerValidator){
$this->issuerValidator = $issuerValidator;
}
this means that you are allowed to change the validator, thus providing the availability to change the property (and validation) without the hassle that you need to create your own class which extends OpenIDConnectClient
and overrides the default validator (if you'd use a method instead of the callable property)
Thanks to @DarkBee for the edit
Upvotes: 3