Re: [PHP] Re: Function variables in classes
Paul van Haren wrote: 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. your assumptions and php's reality differ. symbol names resolution is never tired in the class scope. $a = foobar::bar2; $a(); this is trying to call a function called foobar::bar2, which given that you cant do (parse error): function foobar::bar2() {} whatever munged error message you get regarding 'bar2()' not existing, the fact remains that 'variable' function name functionality has no concept of class scope, the T_PAAMAYIM_NEKUDOTAYIM is not recognized. never has been, probably never will be. class foo { function bar1() { $a = foo::bar2; call_user_func(explode(::,$a)); } static function bar2() { echo __METHOD__,\n; } } $foo = new foo; $foo-bar1(); having no idea what it is that your actually trying to achieve, it's hard to tell whether any percieved limitation is justified. you might consider taking a look at reflection: http://nl2.php.net/reflection BTW: I'm running PHP v5.2.0-8 build and distributed by Debian (etch1). Thanks again and regards, Paul. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Function variables in classes
It seems to be a PHP Bug, because normaly it should very well. but you can solve this problem, by a workarround. using eval($a.();); instead of $a(); in the class. Best regards Sebastian Paul van Haren [EMAIL PROTECTED] schrieb im Newsbeitrag news:[EMAIL PROTECTED] Hi there, I'm trying to execute function variables. This works fine outside class code, but gives a fatal error when run within a class. The demo code is here: ?php function bar1 () { echo Yep, in bar1() right now\n; } function foo1 () { bar1(); $a = bar1; print_r ($a); echo \n; $a(); } class foobar { function bar2 () { echo Yep, in bar2() right now\n; } public function foo2 () { foobar::bar2(); $a = foobar::bar2; print_r ($a); echo \n; $a(); } } foo1(); $fb = new foobar (); $fb-foo2(); ? The error message reads: Fatal error: Call to undefined function foobar::bar2() in /home/paul/demo/demo.php on line 25 Is there anyone out there who can explain what's wrong in this code? Thanks, Paul -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Function variables in classes
Thanks, this helps. The code now works. In case this is truely a bug in PHP, where should I log it? Anything that I should do to make sure that it gets followed up? Regards, Paul -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Function variables in classes
I think you should log it, because it seems to be, and you found this error. Regard Sebastian Paul van Haren [EMAIL PROTECTED] schrieb im Newsbeitrag news:[EMAIL PROTECTED] Thanks, this helps. The code now works. In case this is truely a bug in PHP, where should I log it? Anything that I should do to make sure that it gets followed up? Regards, Paul -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Function variables in classes
On 11/1/07, Sebastian Hopfe [EMAIL PROTECTED] wrote: I think you should log it, because it seems to be, and you found this error. i would not consider this a bug. what paul is asking about is the variable function syntax in php. http://www.php.net/manual/en/functions.variable-functions.php whats happening is php is not resolving the first portion of the variable contents to a class name, nor is it capable of resolving the scope resolution syntax when dynamically evaluating a variable contents in the context of a method call. there is no mention of such support in the manual. consider this fragament (it will not work) class DynamicMethodCaller { public function memFunc() { echo __METHOD__ . PHP_EOL; } public function invoker($dynamicMethod) { $dynamicMethod(); } } $instance = new DynamicMethodCaller(); $instance-invoker('$this-memFunc'); now consider this revision (which works perfectly) class DynamicMethodCaller { public function memFunc() { echo __METHOD__ . PHP_EOL; } public function invoker($dynamicMethod) { $this-$dynamicMethod(); } } $instance = new DynamicMethodCaller(); $instance-invoker('memFunc'); the only difference between this fragment and the one originally posted is the use of static member functions in the original post. here are 2 fragments showing what works and what doesnt when working with static class member functions (doest work) class DynamicMethodCaller { static public function memFunc() { echo __METHOD__ . PHP_EOL; } public function invoker($dynamicMethod) { $dynamicMethod(); } } $instance = new DynamicMethodCaller(); $instance-invoker('DynamicMethodCaller::memFunc'); (works) class DynamicMethodCaller { public function memFunc() { echo __METHOD__ . PHP_EOL; } public function invoker($dynamicMethod) { self::$dynamicMethod(); } } $instance = new DynamicMethodCaller(); $instance-invoker('memFunc'); in general the use of eval() should be avoided unless absolutely necessary. in this case it is not necessary; just use the syntax supported by the interpreter and youre good to go. -nathan
Re: [PHP] Re: Function variables in classes
Sebastian Hopfe wrote: I think you should log it, because it seems to be, and you found this error. it's not a bug - especially because nobody has bothered to mention the php version. I'm guessing that it's being run on a version php5, in which case the fatal error seems correct - you have a non-static method defined and then you are trying to call that method statically which is not allowed (IIRC whether this works or not depends on the minor version of php5 your running - something the OO-purism gang forced on us, actually the restriction may have been removed again - I've completely lost track of the state of the exact OO rules in any given version of php5) Regard Sebastian Paul van Haren [EMAIL PROTECTED] schrieb im Newsbeitrag news:[EMAIL PROTECTED] Thanks, this helps. The code now works. In case this is truely a bug in PHP, where should I log it? Anything that I should do to make sure that it gets followed up? Regards, Paul -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Function variables in classes
On 11/1/07, Jochem Maas [EMAIL PROTECTED] wrote: Sebastian Hopfe wrote: I think you should log it, because it seems to be, and you found this error. it's not a bug - especially because nobody has bothered to mention the php version. this issue has the same implications in php4 and php5, because its about variable functions and phps inability to resolve class names or references from the contents of a variable when the variable is used in the context of a class method invocation. I'm guessing that it's being run on a version php5, in which case the fatal error seems correct - you have a non-static method defined and then you are trying to call that method statically which is not allowed (IIRC whether this works or not depends on the minor version of php5 your running - something the OO-purism gang forced on us, actually the restriction may have been removed again - I've completely lost track of the state of the exact OO rules in any given version of php5) i dont know what the version granularity is either regarding the use of static class methods being invoked when the methods are not declared static. i dont think ive ever seen a fatal error raised for that, and at any rate, the error clearly indicates that it pertains to the misuse of static method, in that case, which is not the case in the error message reported earlier. Error raised when invoking a method not defined as static from a static context: (5.2.4-pl2-gentoo) Strict standards: Non-static method Foo::nonStaticMethod() should not be called statically in /home/nathan/workspace/sacd/svn/itc-dev/testStaticCall2.php on line 14 (original error message from this thread) Fatal error: Call to undefined function foobar::bar2() in /home/paul/demo/demo.php on line 25 this strengthens the case that i made earlier, namely this issue is a result of phps lack of resolving class names and references when they are embedded in a string that is used as a variable function. here is a code fragment to test the static method calls out against any version of php5. ?php class Foo { static public function staticMethod() { echo __METHOD__ . PHP_EOL; } public function nonStaticMethod() { echo __METHOD__ . PHP_EOL; } } Foo::staticMethod(); Foo::nonstaticMethod(); $foo = new Foo(); $foo-staticMethod(); $foo-nonstaticMethod(); ? on php 5.2.4 you will have to enable E_STRICT to see the warning. error_reporting = E_ALL | E_STRICT -nathan
Re: [PHP] Re: Function variables in classes
On Thu, 2007-11-01 at 16:10 +0100, Jochem Maas wrote: I've completely lost track of the state of the exact OO rules in any given version of php5) Haha, you too eh!? BTW, I finally bothered to download PHP 5.2.4 last night and checked it out. This is the first time I've seen a speed improvement over PHP4 for my work. Most of my pages gained a 10% to 20% load time improvement. One thing that was odd though was when I built a few sites from the shell using InterJinn's template engine, the time spent doubled. So I guess I win some and lose some :) Page load time is more important since I don't often build whole sites from scratch. Still perplexing though what could have slowed down so much... I'll have to dig into it with a profiler sometime when I have more free time. Cheers, Rob. -- ... SwarmBuy.com - http://www.swarmbuy.com Leveraging the buying power of the masses! ... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Function variables in classes
Robert Cummings wrote: On Thu, 2007-11-01 at 16:10 +0100, Jochem Maas wrote: I've completely lost track of the state of the exact OO rules in any given version of php5) Haha, you too eh!? BTW, I finally bothered to download PHP 5.2.4 last night and checked it out. This is the first time I've seen a speed improvement over PHP4 for my work. Most of my pages gained a 10% to 20% load time improvement. One thing that was odd though was when I built a few sites from the shell using InterJinn's template engine, the time spent doubled. So I guess I win some and lose some :) Page load time is more important since I don't often build whole sites from scratch. Still perplexing though what could have slowed down so much... I'll have to dig into it with a profiler sometime when I have more free time. interesting info - thanks for the heads up. Cheers, Rob. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Function variables in classes
On Thu, 2007-11-01 at 17:16 +0100, Jochem Maas wrote: Robert Cummings wrote: On Thu, 2007-11-01 at 16:10 +0100, Jochem Maas wrote: I've completely lost track of the state of the exact OO rules in any given version of php5) Haha, you too eh!? BTW, I finally bothered to download PHP 5.2.4 last night and checked it out. This is the first time I've seen a speed improvement over PHP4 for my work. Most of my pages gained a 10% to 20% load time improvement. One thing that was odd though was when I built a few sites from the shell using InterJinn's template engine, the time spent doubled. So I guess I win some and lose some :) Page load time is more important since I don't often build whole sites from scratch. Still perplexing though what could have slowed down so much... I'll have to dig into it with a profiler sometime when I have more free time. interesting info - thanks for the heads up. Yeah, I'm going to make it my default development version now :) Cheers, Rob. -- ... SwarmBuy.com - http://www.swarmbuy.com Leveraging the buying power of the masses! ... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Function variables in classes
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. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php