Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

150
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda