[PHP] non static function called as static one
Hello everyone, I'm wondering if the following behaviour is a bug or a feature. The case is quite complex, so let me explain my point of view. here is the source : ?php class MyTest { public function myfunc() { echo get_class($this); } } class MySecondTest { public function test() { MyTest::myfunc(); } } $test = new MySecondTest(); $test-test(); //output: MySecondTest ? Let me explain : In this case, $this is MySecondTest, which is relevant as it is the last object context. But to my mind, this code should not work like this. Imagine you are the developer of function MyTest. You want your code to interact with other classes and being bugproof. 'MyTest' class here seems OK: $this is expected to be 'MyTest' because function myfunc() is expected to be called in a non-static context. Programmer of the second function created this bug and this unattended behaviour. Maybe this can be done : 1/ Forbid calling the function in static context (How can I test this ? $this is not NULL there !). 2/ (or/and) Raise a warning or an error if a non static function is called as a static one 3/ Create two functions with the same name, one static and the other one not. Unfortunately, this can't be done (yet ?). What do you think ? What's your point of view on this ? I want your feedbacks before opening a bug ticket, as it is not strictly a bug... Olivier
Re: [PHP] non static function called as static one
Olivier Doucet schreef: Hello everyone, I'm wondering if the following behaviour is a bug or a feature. The case is quite complex, so let me explain my point of view. here is the source : ?php class MyTest { public function myfunc() { echo get_class($this); } } class MySecondTest { public function test() { MyTest::myfunc(); } } $test = new MySecondTest(); $test-test(); //output: MySecondTest ? Let me explain : In this case, $this is MySecondTest, which is relevant as it is the last object context. But to my mind, this code should not work like this. Imagine you are the developer of function MyTest. You want your code to interact with other classes and being bugproof. 'MyTest' class here seems OK: $this is expected to be 'MyTest' because function myfunc() is expected to be called in a non-static context. Programmer of the second function created this bug and this unattended behaviour. Maybe this can be done : 1/ Forbid calling the function in static context (How can I test this ? $this is not NULL there !). actually I thought that the engine died in situations like this. I don't see any change to the behaviour in 5.3 either. 2/ (or/and) Raise a warning or an error if a non static function is called as a static one develop with error_reporting set to E_ALL | E_STRICT, then you'll get a big fat warning about it 3/ Create two functions with the same name, one static and the other one not. Unfortunately, this can't be done (yet ?). will never happen (where 'never' = 'very very very long time, at the very least' What do you think ? What's your point of view on this ? I want your feedbacks before opening a bug ticket, as it is not strictly a bug... I think it's inconsistent implementation. I would classify it as a bug, especially given the general trend to strict/pure OO concepts in php. there maybe underlying technical issue with the engine that means this can't be easily fixed. I would suggest asking on internals to see what their opinion is. the pragmatic solution is to not call non-static functions using static syntax. Olivier -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] non static function called as static one
Hi Jochem, 2/ (or/and) Raise a warning or an error if a non static function is called as a static one develop with error_reporting set to E_ALL | E_STRICT, then you'll get a big fat warning about it Yes, that's what I'm using right now. Although, that's not the highlighted problem. [...] the pragmatic solution is to not call non-static functions using static syntax. Well, this is the answer I didn't want :) Thank you for your feedback ! Olivier
Re: [PHP] non static function called as static one
Olivier Doucet schreef: Hi Jochem, 2/ (or/and) Raise a warning or an error if a non static function is called as a static one develop with error_reporting set to E_ALL | E_STRICT, then you'll get a big fat warning about it Yes, that's what I'm using right now. Although, that's not the highlighted problem. if your not gettting a 'Strict Warning' when calling a non-static method statically then error_reporting doesn't include E_STRICT. I tested this with your example code on the cmdline to make sure. [...] the pragmatic solution is to not call non-static functions using static syntax. Well, this is the answer I didn't want :) I here that alot :-) Thank you for your feedback ! Olivier -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] non static function called as static one
Jochem Maas wrote: Olivier Doucet schreef: Hi Jochem, 2/ (or/and) Raise a warning or an error if a non static function is called as a static one develop with error_reporting set to E_ALL | E_STRICT, then you'll get a big fat warning about it Yes, that's what I'm using right now. Although, that's not the highlighted problem. if your not gettting a 'Strict Warning' when calling a non-static method statically then error_reporting doesn't include E_STRICT. I tested this with your example code on the cmdline to make sure. [...] the pragmatic solution is to not call non-static functions using static syntax. Well, this is the answer I didn't want :) I here that alot :-) Thank you for your feedback ! Olivier poor internal developers, they do have a tough job the message is pretty clear: Strict Standards: Non-static method MyTest::myfunc() should not be called statically, assuming $this from incompatible context in.. but the functionality is pretty weird, took me a while to get my head around.. until i came up with the scenario: what if this 'error' halted the compiler and threw a proper error then I imagined the massive outcry from the developers of millions of poorly coded scripts then I remembered php 4 and ran this: ?php error_reporting(E_STRICT); class MyTest { function myfunc() { echo get_class($this); } } class MySecondTest { function test() { MyTest::myfunc(); } } $test = new MySecondTest(); $test-test(); //output: MySecondTest ? and it all became clear mental though, part of me wishes they'd forked php at 4 to save all the lame syntax and weirdness. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] non static function called as static one
Nathan Rixham wrote: Jochem Maas wrote: Olivier Doucet schreef: mental though, part of me wishes they'd forked php at 4 to save all the lame syntax and weirdness. after thought.. I wish they'd forked it to OO and procedural, then us OO guys could have phpoo and be happy, and the procedural guys could have php and be happy because the oo one contained the word poo. :) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] non static function called as static one
Nathan Rixham schreef: Nathan Rixham wrote: Jochem Maas wrote: Olivier Doucet schreef: mental though, part of me wishes they'd forked php at 4 to save all the lame syntax and weirdness. after thought.. I wish they'd forked it to OO and procedural, then us OO guys could have phpoo and be happy, and the procedural guys could have php and be happy because the oo one contained the word poo. lol, and to think we'd be neck deep in it every day :-P :) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php