Re: non static function called as static one

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux