php.net |  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
Votes:1
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
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
2 + 48 = ?
Subscribe to this entry?

 
 [2005-04-10 10:14 UTC] bart at mediawave dot nl
Description:
------------
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.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

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

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;
$mountainBike->addPadel($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] jani@php.net
-Package: Feature/Change Request +Package: Class/Object related
 [2010-12-22 15:35 UTC] johannes@php.net
-Status: Open +Status: Bogus
 [2010-12-22 15:35 UTC] johannes@php.net
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: Thu Mar 28 12:01:27 2024 UTC