Search code examples
phpinheritanceconstantsstatic-variables

PHP OOP - constant vs static variables?


In PHP, What is is the difference between:

  1. Constants and static variables?
  2. Extending a class and creating its object?

I know how they can be used, but I can't clearly distinguish between them.


Solution

  • Static is for:

    class properties or methods as static makes them accessible without needing an instantiation of the class

    So, the value returned by a static member may differ. For example, you can call a static method with different result depending of what parameters you pass to it.

    Constants value:

    must be a constant expression, not (for example) a variable, a property, a result of a mathematical operation, or a function call.

    So, it always return the same result when you call it

    About create an object and extending a class, when you "create an object" you make an instance of a class. When you extend a class, you create an other class who:

    inherits all of the public and protected methods from the parent class. Unless a class overrides those methods, they will retain their original functionality.

    I hope it help you.