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

  • Home
  • SEARCH
  • 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 915339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:52:16+00:00 2026-05-15T17:52:16+00:00

Is it possible to set the parent of the class? I.e. an instance of

  • 0

Is it possible to set the parent of the class? I.e. an instance of the parent class gets instantiated during runtime and then a child class instance extending a certain parent instance gets created. For example:

class A {
    var $test = 'default';
}

class B extends A {
    public function __contsruct(A $a) {
        parent = $a; //does not work
    }
}

$a = new A();
$a->test = 'changed';
$b = new B($a);
$b->test == 'changed'; //should be true

I know that I could just $b = new B(); $b->test = 'changed', but that’s not what I’m asking about.

  • 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-05-15T17:52:17+00:00Added an answer on May 15, 2026 at 5:52 pm

    What you’ve asked for is not possible in base PHP. There are a few ways to do similar things.

    You could use the highly experimental runkit extension. The runkit_class_emancipate and runkit_class_adopt functions should work. However, they operate on entire classes, not instances of a class. This probably limits their usefulness for your application.

    If you’re trying to emulate the expandable class features of other languages, like Ruby and Perl, runkit_method_add and related functions might be more suitable. Again, however, it still operates on entire classes.

    The normally accepted "PHP way" to do things like this is via __call. In fact, with anonymous functions in 5.3, you can do something like…

    class Bar {
        public function say($thing) {
            echo "Bar::say says: $thing\n";
        }
    }
    
    class Foo extends Bar {
        private $extensions = array();
    
        public function addExtension($func_name, $func) {
            $this->extensions[ $func_name ] = $func;
        }
    
        public function __call($func_name, $arguments) {
            array_unshift($arguments, $this);
            if(array_key_exists($func_name, $this->extensions))
                call_user_func_array($this->extensions[ $func_name ], $arguments);
        }
    }
    
    $f = new Foo();
    $anon = function($obj, $string){ $obj->say($string); };
    $f->addExtension('example', $anon);
    $f->example("Hello, world!");
    

    You’ll note in __call and that in creating the anonymous function that the first argument becomes the instance. That’s because PHP 5.3’s implementation of anonymous functions can’t reference $this. That also means that they can’t reference protected or private members of the class. This can be corrected by cloning the instance and using Reflection to expose the protected and private members. See this comment on the anonymous function manual page for an example implementation.

    Because of limitations of PHP, you can’t directly assign an anonymous function to a property and call it. This example will not work:

    class WillNotWork {
        public $fatal_error;
    }
    
    $broken = new WillNotWork();
    $anon = function($arg) { echo "I'm about to create a {$arg} error!"; };
    $broken->fatal_error = $anon;
    $broken->fatal_error("fatal");
    // Fatal error: Call to undefined method WillNotWork::fatal_error()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to refer to a parent object in a child object using
This is my setup: public class Parent { public virtual int Id { get;
I am trying to make a style that only gets applied if the parent
I have a control that looks along the lines of this: public class MyControl
Possible Duplicate: Enum “Inheritance” I have a number of classes which extend an abstract
I'm trying to set my control over the Outlook Mailgrid with SetParent and resize
I have entities: public class Document : PersistentObjectBase { public virtual long? FolderId {
I happen to have a database with a table that holds all possible combination
i am trying to add a container having some UI components as child in
Just today I noticed a strange behavior in an object model that was previously

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.