Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

262
Visualizações
Callable function as PHP class property

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.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda