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

98
Visualizações
PHP Class Extends a string variable

Is it possible to declare a class and have it extend a variable?

class Child extends $parentClass {}
about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Yes, it is with eval. But it is not recommended.

<?php
function dynamic_class_name() {
    if(time() % 60)
        return "Class_A";
    if(time() % 60 == 0)
        return "Class_B";
}
eval(
    "class MyRealClass extends " . dynamic_class_name() . " {" . 
    # some code string here, possibly read from a file
    . "}"
);
?>

Is Eval an evil?! Read this.

about 4 years ago · Santiago Trujillo Relatório

0

no you cant.

this is because in a normal definition:

class A extends SomeOtherClass {
}

SomeOtherClass is a namespace reference, not an instance of the class. And you cant use a variable to provide that namespace because variables in PHP are defined at runtime, whereas classes are defined in the compile phase.

But... if youre using PHP7, you can do this:

$newClass = new class extends SomeOtherClass {};

which is not exactly what you want, but goes some way.

about 4 years ago · Santiago Trujillo Relatório

0

A Class can not extend from a variable , You need to wrap the variable inside a class to extend . Hope this helps

about 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