[ create a new paste ] login | about

Link: http://codepad.org/HK6Oo4xL    [ raw code | output | fork ]

PHP, pasted on Jul 7:
<?php
class A {

	protected $val = 'test';

	public function get() {
		return $this->val;
	}

	public function set($val) {
		return $this->val = $val;
	}
}

class B {
	protected $Obj;
	public function __construct(A $obj) {
		$this->Obj = $obj;
	}

	public function getObj() {
		return $this->Obj;
	}
}

class C {
	protected $array;
	public function __construct($array) {
		$this->array = $array;
	}

	public function getArray($item) {
		return $this->array[$item];
	}
}

$a = new A();
$arr = array('obj' => $a);
$b = new B($a);
$c = new C($arr);

echo 'Before', PHP_EOL, 'Object : ', $a->get(), PHP_EOL, 'Object in object : ', $b->getObj()->get(), PHP_EOL, 'Object in array : ', $arr['obj']->get(), PHP_EOL, 'Object in array in object : ', $c->getArray('obj')->get(), PHP_EOL;
$a->set('modified');
echo 'After', PHP_EOL, 'Object : ', $a->get(), PHP_EOL, 'Object in object : ', $b->getObj()->get(), PHP_EOL, 'Object in array : ', $arr['obj']->get(), PHP_EOL, 'Object in array in object : ', $c->getArray('obj')->get(), PHP_EOL;


Output:
1
2
3
4
5
6
7
8
9
10
Before
Object : test
Object in object : test
Object in array : test
Object in array in object : test
After
Object : modified
Object in object : modified
Object in array : modified
Object in array in object : modified


Create a new paste based on this one


Comments: