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

149
Visualizações
Use parent outside of class php

Given

abstract class A {
  public function __get($key) {
    ...
  }
}

class B extends A {
  public function __get($key) {
    ...
  }
}

class C {
  public function test($bObject) {
    //Call the __get function of A on the object of B
  }
}

How would I call A::__get on an object of B from C::test? I've looked into using a callable on class A::__get and binding it to the object of class B, or using parent, but neither of those methods seem to help.

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

0

In PHP one way you can do that is with reflection (introspection or also meta-programming), as by the class/object design, the encapsulation normally forbids it.

Given the following from the class definitions in question:

$b = new B();
$b->test;

It works by obtaining the closure of the parents method with the B object (e.g. the $bObject in your question or $b in the example here) and then calling it:

(new ReflectionObject($b))
    ->getParentClass()
    ->getMethod('__get')
    ->getClosure($b)('test'); // or: ->invoke($b, 'test');

Demo: https://3v4l.org/0tj26 and output for PHP 7.3.0 - 7.3.29, 7.4.0 - 7.4.21, 8.0.0 - 8.0.8:

string(8) "B::__get"
string(4) "test"
string(8) "A::__get"
string(4) "test"
over 4 years ago · Santiago Trujillo Relatório

0

Try using the ReflexionClass and the getParent method.

$objectB;
$key = 'key';

$reflectorB = new ReflectionObject($objectB);
$reflectorA = $reflectorB->getParentClass();
$method = $reflectorA->getMethod('__get');
return $method->invoke($objectB, $key);
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