Re: Re: Function variables in classes

From: Date: Fri, 02 Nov 2007 06:51:27 +0000
Subject: Re: Re: Function variables in classes
References: 1 2 3 4 5  Groups: php.general 
Request: Send a blank email to php-general+get-263957@lists.php.net to get a copy of this message
OK guys, thanks for all your inputs. Based on your guidance, I have tested the following code with a series of variations: class foobar { function bar2 () { echo "Yep, in bar2() right now\n"; } public function foo2 () { $a = "bar2"; // Experiment 0 $a(); // Fatal error } } And the variations: $a = "bar2"; // Experiment 0 $a(); // Fatal error: Call to undefined function bar2() $a = "foobar::bar2"; // Experiment 1 $a(); // Fatal error: Call to undefined function bar2() $a = "bar2"; // Experiment 2 eval($a."();"); // Fatal error: Call to undefined function bar2() $a = "foobar::bar2"; // Experiment 3 eval($a."();"); // Works but far from elegant $a = "bar2"; // Experiment 4 $this->$a(); // Works fine $a = "bar2"; // Experiment 5 self::$a(); // Works fine So, I have a working solution right now. But I still don't understand the essence of the differences between experiment #1 and #4. Also, I don't understand the need to specify the class name in experiment #3, coming from #2. Functions bar2() and foo2() are part of the same class foobar, and I would assume that the name 'bar2' would be in scope of the function foo2. BTW: I'm running PHP v5.2.0-8 build and distributed by Debian (etch1). Thanks again and regards, Paul.

« previous php.general (#263957) next »