Martin Scotta

On Mon, Apr 25, 2011 at 4:52 AM, Alessandro Nadalin <
alessandro.nada...@gmail.com> wrote:

> 2011/4/24 Etienne Kneuss <col...@php.net>:
> > Hi,
> >
> > On Apr 24 22:13:47, Ángel González wrote:
> >> reeze wrote:
> >> > Hi,
> >> > I am not sure it's the right place to discuss this. someday I found I
> call a static method _instancely_.
> >> > the method is just a helper method when reviewing my code. I know I do
> the wrong thing, but PHP doesn't
> >> > complain about it. then I do some tests like below:
> >>
> >> A few corrections to your test case so it actually works.
> >> > <?php
> >> > error_reporting(E_ALL ^ E_STRICT);
> >> I think you want E_ALL | E_STRICT
> >>
> >> > class A {
> >> > public function static staticFunc() {
> >> This is a parse error. static should be placed before function.
> >>
> >> > echo "static";
> >> > }
> >> > public function instanceFunc() {
> >> > echo "instace";
> >> > }
> >> > }
> >> >
> >> > A::instanceFunct(); // Strict Standards: Non-static method
> A::instanceFunc() ...
> >> And this should be A::instanceFunc();
> >>
> >> > $a = new A();
> >> > $a->staticFunc(); // Just static no E_STRICT error raised
> >> >
> >> > I know it's the wrong way to do like these, maybe there are some
> historical reasons to allow these.
> >> > I just wonder why previous method call raise E_STRICT but later not.
>
> Hi Etienne,
>
> >
> > Nothing wrong with it.
> >
> > The E_STRICT is raised because when you call a non-static method
> > statically, $this will not be defined and that could be a problem (e.g.
> > the method could rely on it). When you call a static method with ->, it
> > remains a static call, $this will not be defined anyway, and there is
> > absolutely no problem with it.
>
> I can see your point, but I think we should drop this "feature" not to
> let developers have N ways of doing the same things: if you declare a
> method as static, you should be able to only call it with a static
> call, not hrough an object.
>
> PHP usually lets you do the same thing in a few ways, and that's a bit
> frustrating, because we should force some kind of best practices for
> the language: not .advise, but *force*.
>
> My 2 cents,
>

Back in PHP4 it was the only way to "simulate" an static call, but
nowadays it really don't make sense at all.

class Foo {
     static function toString(Bar $bar) {
          return 'Foo::toString($bar)';
     }
     function toString() {
          return '$this->toString()';
     }
}

$foo = new Foo();
echo $foo->toString(); // instance
echo Foo::toString(); // class

PHP will complain about the 2nd method (can't redefine function) but for me
are 2 completely different methods.
I belive the current Object Model could be improved. isn't?


> >
> >
> > Best,
> >
> >> >
> >> > Yes, something could be done doesn't means we should, but we could
> stop things like happened.
> >>
> >> I think it may be related to inheritance.
> >>
> >>
> >>
> >> --
> >> PHP Internals - PHP Runtime Development Mailing List
> >> To unsubscribe, visit: http://www.php.net/unsub.php
> >>
> >
> > --
> > PHP Internals - PHP Runtime Development Mailing List
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
>
>
>
> --
> Nadalin Alessandro
> www.odino.org
> www.twitter.com/_odino_
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

Reply via email to