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

256
Visualizações
Method followed by colon and a question mark and a class

Strange, but couldn't find this on SO, maybe someone can help.

What does this syntax mean in PHP when we have a colon followed by a question mark and then another class name when a method is being defined?

public function someMethod(): ?SomeClass

Full function just in case with oriignal names

    public function getParentProject(): ?ParentProjectTester
    {
        return $this->parentProject;
    }
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

This syntax means that the method is forced to return NULL (what the question mark stands for) or and instance of the declared class. Refer to: php docs.

If something else is returned, this will cause an error like: Return value of Name\Space::method() must be of the type ..., ... returned in /Path/To/File.php on line ....

It's good practice to use this strict typing in object oriented programming so the code is more unambiguous and less sensitive to errors.

But it's not obligated to use, your code will work perfectly fine if you remove the return type and just use public function getParentProject().

over 4 years ago · Santiago Trujillo Relatório

0

That is a return type declaration, telling you the type it should return: https://www.brainbell.com/php/type-declarations-hints.html#return-type

The question mark means it's nullable, meaning it might be null and not that type.

over 4 years ago · Santiago Trujillo Relatório

0

The question mark indicates that the function may return in instance of that class, or it may return null. It's called a nullable type and you can read about it here.

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