-1

Code beforehand fully valid code from 5.0.0 (without E_STRICT or E_DEPRECATED)

class A{
    static public function b() {
    }

    public function c(){
        $this->b();
    }
}

$d = new A();
$d->c();
$d->b();

It's looks like inconsistent behaviour because you cannot use static properties from instance.

The PHP way is to steal and borrow from other languages whenever possible ...

But I cannot find any programming language that supports similar behavior.

Why does PHP support it? What is the point of calling static methods as non-static?

Some explanation from support: Expected behavior

6
  • 1
    I would assume because it was misscorrectly tagged as Duplicate, when it wasn't @PaulCrovella. Commented Oct 23, 2018 at 11:44
  • I agree 100% @PaulCrovella. I'm only speculating as to why the OP did what he did. Commented Oct 23, 2018 at 11:47
  • Java is an example of such a language. Commented Oct 23, 2018 at 11:55
  • @PaulCrovella, it was closed as duplicated. It's hard to restore question from there. Deleted question should not appear in reopen queue. Commented Oct 23, 2018 at 12:34
  • @sectus The easiest way is to ping @Phil, and explain to him why it's not a reason. Had you explained in a comment/edit that this is a request for reasoning, and that dupe target has absolutely no developer reasoning, I'm sure others would agree with you (I personally would have voted to reopen). Commented Oct 23, 2018 at 12:40

2 Answers 2

2

Actually, C++ and Java support this. It seems the PHP developers, after discussion, decided on implementation to match them.

After a bit of digging, I found this thread from February 2004, which is essentially their discussion about the implementation choices. Important tidbits from the discussion:

From Cristiano Duarte:

C++ allows $a->bar() when bar() is a static method (yes, it is called in a static context there too).

IMO, there should be no error, warning or notice here.

I Agree. PHP is fine the way it is.

From Art:

Regardless of the final implementation, I think access to static methods and static class variables should be consistent. Currently, you cannot access a class variable via an object instance.

And for what it's worth, I see no reason why static methods cannot be called from objects. Follow Java/C++ in this case.

Ultimately, a final decision From Wez:

Please drop this thread; we're not changing the behaviour of static.

Sign up to request clarification or add additional context in comments.

3 Comments

It is a little bit tricky to call static method of parent class of instance in Java.
@sectus I agree, and the full thread goes into a little bit more detail on that. (I recommend giving it a full read, if you're curious).
Sure, I am curious.
1

It looks to me that it is just a syntax consideration here. Nothing here is inconsistent with the logic of static methods, it's still impossible to use $this in your static function, and therefore the function will not have access to instance properties or methods. It feels more like a shortcut than an inconsistency to me.

I have no use case of that, but I guess someone may find it useful with objects created with dynamic class names: you can still use the function even if you don't know it's class name.

2 Comments

Why static properties cannot be used in the same way?
this can seem inconsistent, yes, but it's actually possible to get a static property from an instance with the syntax $d::$b, though you cannot use the arrow here, which is a bit inconsistent syntax i admit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.