Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

148
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!