En/na Jochem Maas ha escrit:
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 !
Have you tried this ?
In my php4 installation it doesn't work !
$ php4 -version
PHP 4.4.2-1.1 (cli) (built: Jun 20 2006 02:33:21)
Copyright (c) 1997-2006 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php