PHP OOP Static Methods

PHP OOP Static Methods
- You can call it without creating an object
- Use the keyword
static Access usingClassName::methodName()
Basic Static Method Example
Static Method Inside Class
You can still call static methods from inside the class using self::.
Static Method with Parameters
Static Method vs Object Method
| Type | Called By | Use |
|---|---|---|
| Static Method | ClassName::method() | Utility functions, helpers |
| Object Method | $object->method() | When data belongs to object |
Static Methods Cannot Use $this
This will cause an error:
Calling Static Methods from Child Classes (Inheritance)
Overriding Static Methods in Child Class
Late Static Binding (Important)
Use static:: when you want the child class’s static method/property.
Real-Life Example – Helper Class
Real-Life Example – DB Connection (Singleton Pattern)
Summary (Easy to Remember)
| Feature | Meaning |
|---|---|
| static method | Belongs to class, not object |
| Access using | ClassName::method() |
No $this | Cannot use $this inside static |
| Good for | Utility methods, helpers, math functions |
| Works with inheritance | Yes |
