Re: References challenge with PHP4
| From: | Jochem Maas | Date: | Fri, 16 Mar 2007 14:44:20 +0000 |
| Subject: | Re: References challenge with PHP4 | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-250707@lists.php.net to get a copy of this message | ||
Lluis Pamies wrote:
> I've the following code:
>
> 1 <?php
> 2
> 3 class B {
> 4 function B(&$a) {
> 5 $this->a = &$a;
> 6 }
> 7
> 8 function run() {
> 9 print "{$this->a->msg}\n";
> 10 }
> 11 }
> 12
> 13 class A {
> 14 function A() {
> 15 $this->b = new B(&$this);
> 16 }
> 17
> 18 function &getInstance() {
> 19 static $inst;
> 20 if(!isset($inst)) $inst = new A();
if(!isset($inst)) $inst =& new A();
> 21 return $inst;
> 22 }
> 23
> 24 function run() {
> 25 $this->b->run();
> 26 }
> 27 }
> 28
> 29 $a = &A::getInstance();
> 30 $a->msg = "Hello world !";
> 31
> 32 // Some tasks, flow control, scope changes, ...
> 33
> 34 $a = &A::getInstance();
> 35 print "{$a->msg}\n"; // This should print "Hello world !"
> 36 $a->run(); // This should print "Hello world !"
> 37
> 38 ?>
>
> My problem is that I don't know what I need to do in order to print
> the same value in lines 35 and 36. Line 35 prints "Hello world !", but
> in the 36 nothing appears. What I'm doing wrong ?
>
> Thanks for all !
>