PHP allows access to non-static methods using ClassName:: But Why does it throw error when accessing non-static properties using ClassName::?


It does not make sense to access non-static things by class name, unless you also provide an instance somehow.

Non-static things exist individually for each object. E.g. MY car is red, not ALL cars, not just ANY car, but mine.

If you only use Car (the type) without any reference to the specific car, you have not unambiguously specified what you actually want.

For your case, I am not quite sure I understand, and I've not done PHP for years, but maybe the answer is that constructors are always static.

Which makes sense, since you're making a specific object. If it weren't static, you would need an object to create itself.

Show all replies

That's ok, hope the other answers explain the missing details

Reply to this…

(3 answers) Take me to the question