Edit report at http://bugs.php.net/bug.php?id=32654&edit=1

 ID:                 32654
 Updated by:         johan...@php.net
 Reported by:        bart at mediawave dot nl
 Summary:            Magic __parent property (or method)
-Status:             Open
+Status:             Bogus
 Type:               Feature/Change Request
 Package:            Class/Object related
 Operating System:   *
 PHP Version:        *
 Block user comment: N
 Private report:     N

 New Comment:

With this the behavior of the class changes depending on outer influence
which isn't directly obvious. Such magic is hardly maintainable.


Previous Comments:
------------------------------------------------------------------------
[2005-04-10 14:52:42] 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.

------------------------------------------------------------------------
[2005-04-10 12:22:26] he...@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 10:14:44] 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.



------------------------------------------------------------------------



-- 
Edit this bug report at http://bugs.php.net/bug.php?id=32654&edit=1

Reply via email to