|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #32654 Magic __parent property (or method)
Submitted: 2005-04-10 10:14 UTC Modified: 2010-12-22 15:35 UTC
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: bart at mediawave dot nl Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: * OS: *
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: bart at mediawave dot nl
New email:
PHP Version: OS:


 [2005-04-10 10:14 UTC] bart at mediawave dot nl
When writing code with complex object structures I often need a reference to the parent object. Let's say we have an object "parent" that has a property which is an object "child":

class parentClass {

  var $child;
  var $dummyVar;

  function __construct() {
    $this->child = new childClass($this);
    $this->dummyVar = 'hello';


class childClass {

  var $parent;

  function __construct($parent) {
    $this->parent = $parent;
    echo $this->parent->dummyVar;


$example = new parentClass;

// prints: hello

I very often need to create my code like this because I very often need to be able to have a reference to the parent object of an object. 

Shouldn't PHP have such a reference by default? There could always be a Magic reference like:

$myParent = $this->__parent;

Or maybe a Magic method:

$myParent = $this->__parent();

This would then always return the parent object or eventually reach the main script scope.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2005-04-10 12:22 UTC]
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at and the instructions on how to report
a bug at

There is no such thing as a parent object. Use 'parent::'<method>'()' to access methods of the parent.
 [2005-04-10 14:52 UTC] bart at mediawave dot nl
I think my explanation might have been too vague. The "parent" you're referring to (parent::) is for accessing methods and properties when a class extends another class. I'm talking about objects.

A good example is the Document Object Model. Each html tag is represented by an object. Each tag can have child tags. In DOM child tags are called childNodes. In DOM there is also a property for accessing parentNodes via the parentNode property. This is the property I'm talking about.

Another good example is Flash. In Flash you have "movie clips" which are objects. When a movie clip is inside another movie clip you can reference the "parent" movie clip via the this.parent property.

Or another example. let's say we have a bicycle:

$mountainBike = new bicycle;

Of course we want the bicycle to have padels:

$padel = new padel;

Now, let's say we always want the padels to have the same color as the bicycle. In padel we could have some code like:

function __construct() {
  $this->color = $this->__parent->color;

So, as you can see there are such things as parent and child objects. I'm always able to access child objects from within parent objects. I just thought it would be nice if there's also a way to access parent objects from within child objects.
 [2010-11-19 00:27 UTC]
-Package: Feature/Change Request +Package: Class/Object related
 [2010-12-22 15:35 UTC]
-Status: Open +Status: Bogus
 [2010-12-22 15:35 UTC]
With this the behavior of the class changes depending on outer influence which isn't directly obvious. Such magic is hardly maintainable.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Jul 21 05:01:30 2024 UTC