note 104813 modified in language.references.whatdo by danbrown

From: Date: Fri, 08 Jul 2011 20:37:38 +0000
Subject: note 104813 modified in language.references.whatdo by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-181364@lists.php.net to get a copy of this message
in PHP you don't really need pointer anymore if you want to share an object across your program <?php class foo{ protected $name; function __construct($str){ $this->name = $str; } function __toString(){ return 'my name is "'. $this->name .'" and I live in "' . __CLASS__ . '".' . "\n"; } function setName($str){ $this->name = $str; } } class MasterOne{ protected $foo; function __construct($f){ $this->foo = $f; } function __toString(){ return 'Master: ' . __CLASS__ . ' | foo: ' . $this->foo . "\n"; } function setFooName($str){ $this->foo->setName( $str ); } } class MasterTwo{ protected $foo; function __construct($f){ $this->foo = $f; } function __toString(){ return 'Master: ' . __CLASS__ . ' | foo: ' . $this->foo . "\n"; } function setFooName($str){ $this->foo->setName( $str ); } } $bar = new foo('bar'); print("\n"); print("Only Created \$bar and printing \$bar\n"); print( $bar ); print("\n"); print("Now \$baz is referenced to \$bar and printing \$bar and \$baz\n"); $baz =& $bar; print( $bar ); print("\n"); print("Now Creating MasterOne and Two and passing \$bar to both constructors\n"); $m1 = new MasterOne( $bar ); $m2 = new MasterTwo( $bar ); print( $m1 ); print( $m2 ); print("\n"); print("Now changing value of \$bar and printing \$bar and \$baz\n"); $bar->setName('baz'); print( $bar ); print( $baz ); print("\n"); print("Now printing again MasterOne and Two\n"); print( $m1 ); print( $m2 ); print("\n"); print("Now changing MasterTwo's foo name and printing again MasterOne and Two\n"); $m2->setFooName( 'MasterTwo\'s Foo' ); print( $m1 ); print( $m2 ); print("Also printing \$bar and \$baz\n"); print( $bar ); print( $baz ); ?> --was-- // in PHP you don't really need pointer anymore // if you want to share an object across your program class foo{ protected $name; function __construct($str){ $this->name = $str; } function __toString(){ return 'my name is "'. $this->name .'" and I live in "' . __CLASS__ . '".' . "\n"; } function setName($str){ $this->name = $str; } } class MasterOne{ protected $foo; function __construct($f){ $this->foo = $f; } function __toString(){ return 'Master: ' . __CLASS__ . ' | foo: ' . $this->foo . "\n"; } function setFooName($str){ $this->foo->setName( $str ); } } class MasterTwo{ protected $foo; function __construct($f){ $this->foo = $f; } function __toString(){ return 'Master: ' . __CLASS__ . ' | foo: ' . $this->foo . "\n"; } function setFooName($str){ $this->foo->setName( $str ); } } $bar = new foo('bar'); print("\n"); print("Only Created \$bar and printing \$bar\n"); print( $bar ); print("\n"); print("Now \$baz is referenced to \$bar and printing \$bar and \$baz\n"); $baz =& $bar; print( $bar ); print("\n"); print("Now Creating MasterOne and Two and passing \$bar to both constructors\n"); $m1 = new MasterOne( $bar ); $m2 = new MasterTwo( $bar ); print( $m1 ); print( $m2 ); print("\n"); print("Now changing value of \$bar and printing \$bar and \$baz\n"); $bar->setName('baz'); print( $bar ); print( $baz ); print("\n"); print("Now printing again MasterOne and Two\n"); print( $m1 ); print( $m2 ); print("\n"); print("Now changing MasterTwo's foo name and printing again MasterOne and Two\n"); $m2->setFooName( 'MasterTwo\'s Foo' ); print( $m1 ); print( $m2 ); print("Also printing \$bar and \$baz\n"); print( $bar ); print( $baz ); http://php.net/manual/en/language.references.whatdo.php

« previous php.notes (#181364) next »