Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7720809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:45:50+00:00 2026-06-01T03:45:50+00:00

I am using Reflections to adjust various values in objects, and I have an

  • 0

I am using Reflections to adjust various values in objects, and I have an object who’s parent I need to adjust.

For example:

class Ford extends Car
{
    private $model;
}

class Car
{
    private $color;
}

I can easily use Reflection to change the model, but how can I separate the parent from the child, so that I can use Reflection on the parent?

Some psuedo code for what I’m hoping is possible:

$ford = new Ford();

$manipulator = new Manipulator($ford);

$manipulator->set('model','F-150');
$manipulator->setParentValue('color','red');

class Manipulator
{
    public function __construct($class) {
        $this->class = $class;
        $this->reflection = new \ReflectionClass($class);
    }

    public function set($property,$value) {
        $property = $this->reflection->getProperty($property);
        $property->setAccessible(true);
        $property->setValue($this->class,$value);
    }

    public function setParentValue() {

        $parent = $this->reflection->getParent();

        $property = $this->reflection->getProperty($property);
        $property->setAccessible(true);

        // HOW DO I DO THIS?

        $property->setValue($this->class::parent,$value);
    }
}

Gist of the question:

In this case, how can I change the $color from outside the object altogether?

Is there something like Ford::parent() or get_parent_object($ford) available?

Note

The objects used above are not the exact scenario, but just used to illustrate the concept. In the real world case, I have a parent/child relationship, and I need to be able to access/change values in each from the outside.

ANSWER

Please check my answer below…I figured it out.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T03:45:51+00:00Added an answer on June 1, 2026 at 3:45 am

    After extensive review, I have found that I can’t access the parent of an object AS AN OBJECT outside of the object itself.

    However, using Reflections, I was able to solve the example posted above:

        <?php
    class Car
    {
        private $color;
    
        public function __construct()
        {
            $this->color = 'red';
        }
    
        public function color()
        {
            return $this->color;
        }
    }
    
    class Ford extends Car
    {
    }
    
    $ford = new Ford();
    
    echo $ford->color(); // OUTPUTS 'red'
    
    $reflection = new ReflectionClass($ford);
    
    $properties = $reflection->getProperties();
    foreach($properties as $property) {
        echo $property->getName()."\n>";
    }
    
    $parent = $reflection->getParentClass();
    
    $color = $parent->getProperty('color');
    $color->setAccessible(true);
    $color->setValue($ford,'blue');
    
    echo $ford->color(); // OUTPUTS 'blue'
    

    See it in action here: http://codepad.viper-7.com/R45LN0

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an abstract class Abstr<T> and an extending class Ext<T> . I need
I am using Reflections to obtain a method from a class that is annotated
I have a simple question. Is there a way ( using reflections I suppose
Using reflection, I need to investigate a user DLL and create an object of
I am learning the reflections concepts in c#. I have a class like this
I am using reflection class to invoke some methods which are on the some
I am using reflection to get the DeclaredFields of a particular class. Is there
I'm currently using reflection to get the declared Fields of a GUI class. However
How to inject method (using reflection) into super class without extending tested class? I
I am using django forms to add a new objects to the db. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.