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

View other answers to this thread

Learn Something New Everyday,
Connect With The Best Developers!

Sign Up Now!

& 500k+ others use Hashnode actively.

Jason Knight's photo

Statics are STATIC, they exist all the time. NON-STATIC means they only exist after you "new' and assign said resultant object to a variable, and will only exist on that variable. They never actually exist in memory on the class.

That's kind of the POINT of static and non-static... so if you're asking this you kind-of missed the point.

Show +1 replies
Jason Knight's photo

The less code you use, the less there is to break

Mohammad Daud Ibrahim If you think about it methods are always static, as their value does not change during runtime and are the same on all instances of said class. Only way to overload them is to create a new class that "extends". Think of them more as being like constants.

Properties on the other hand can have different values for each object instance, with methods they're always the same if the class is the same.

Hence the ability to class overload via typecasting in some languages, or the ability to call a parent class' methods from an extension class.

Mohammad Daud Ibrahim's photo

The Measure of a Man is What He Does with Power.

I think out of all the answers here yours make most sense to me. I think I now understand the why behind it. Thank you very much. 😊

Want to read more?

Browse featured discussions

© 2020 · Hashnode