This is a discussion on Function variables in classes within the PHP General forums, part of the PHP Programming Forums category; On 11/1/07, Jochem Maas <jochem@iamjochem.com> wrote: > > Sebastian Hopfe wrote: > > I ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
On 11/1/07, Jochem Maas <jochem@iamjochem.com> 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 |
|
|||
|
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! .................................................. .......... |
|
|||
|
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. |
|
|||
|
On Nov 1, 2007, at 7:10 AM, Paul van Haren wrote:
> 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 > I don't know if this has been said yet, but in 5.3 they added this: Dynamic static calls: $c = "classname"; $c::someMetod(); |
|
|||
|
On 11/1/07, Eric Butera <eric.butera@gmail.com> wrote:
> > I don't know if this has been said yet, but in 5.3 they added this: > Dynamic static calls: $c = "classname"; $c::someMetod(); apparently it works in 5.2.4 as well: <?php class Foo { static public function staticMethod() { echo __METHOD__ . PHP_EOL; } public function nonStaticMethod() { echo __METHOD__ . PHP_EOL; } } $fooStr = 'Foo'; $fooStr::staticMethod(); Foo::staticMethod(); Foo::nonstaticMethod(); $foo = new Foo(); $foo->staticMethod(); $foo->nonstaticMethod(); ?> -nathan |
|
|||
|
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! .................................................. .......... |
|
|||
|
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. > |
|
|||
|
Hi paul, Why you are trying to use the scope resolution operator see you can use this way simply. Code: ****** <?php class foobar { function bar2 () { echo "Yep, in bar2() right now\n"; } public function foo2 () { $a = "bar2"; // Experiment 0 $a(); // Fatal error } } $foo = new foobar(); $funname = "bar2"; $foo->$funname(); ?> -- View this message in context: http://www.nabble.com/Function-varia...html#a13545465 Sent from the PHP - General mailing list archive at Nabble.com. |
|
|||
|
On 11/2/07, rohini <deepthirohini@gmail.com> wrote:
> > > Hi paul, > Why you are trying to use the scope resolution operator see you can use > this way simply. > > Code: > ****** > <?php > class foobar { > function bar2 () { > echo "Yep, in bar2() right now\n"; > } > > public function foo2 () { > $a = "bar2"; // Experiment 0 > $a(); // Fatal error > } > } > $foo = new foobar(); > $funname = "bar2"; > $foo->$funname(); > ?> > perhaps hes not working with an instance of the class. it is perfectly reasonable to use the scope resolution operator in a variable function. you just cant include the scope resolution operator in the string that gets handed to the interpreter. take a look at the code fragments from yesterday for examples. -nathan |