note 84873 modified in keyword.parent by danbrown
| From: | danbrown@php.net | Date: | Sun, 03 Aug 2008 05:14:02 +0000 |
| Subject: | note 84873 modified in keyword.parent by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-143090@lists.php.net to get a copy of this message | ||
To simulate traditional C# like parent access, child classes can be defined in the parent's
constructor and linked via explict definition. ($this->b->par = $this;)
<?php
class A {
var $foo;
var $b;
function A(){
$this->foo = "foo!";
$this->b = new B;
$this->b->par = $this;
}
function bar(){
print("bar!");
}
};
class B {
var $par;
function B(){
}
function bar(){
print( $this->par->foo );
$this->par->bar();
}
};
$test = new A;
$test->b->bar();
?>
This returns:
foo!bar!
demonstrating both variable and function access via the parent.
--was--
To simulate traditional C# like parent access, child classes can be defined in the parent's
constructor and linked via explict definition. ($this->b->par = $this;)
========================================
class A {
var $foo;
var $b;
function A(){
$this->foo = "foo!";
$this->b = new B;
$this->b->par = $this;
}
function bar(){
print("bar!");
}
};
class B {
var $par;
function B(){
}
function bar(){
print( $this->par->foo );
$this->par->bar();
}
};
$test = new A;
$test->b->bar();
===================================
This returns:
foo!bar!
demonstrating both variable and function access via the parent.
http://php.net/manual/en/keyword.parent.php