MVC Lesson 4: OOP one-liner concepts in PHP

Here are some OOP one-liner concepts in PHP with clear and concise definitions.

Static variable – Belongs to the class and can be accessed without creating an object.

Static method – Can be called directly using the class name without an object.

Final method – Cannot be overridden by any subclass.

Abstract class – Cannot be instantiated and is meant to be extended by other classes.

Abstract method – Declared without a body in an abstract class and must be implemented in child classes.

Public – Accessible from anywhere using the class object.

Private – Accessible only within the class where it is declared.

Protected – Accessible within the class and its subclasses.

Extends – Used by a class to inherit from another class.

:: (Scope Resolution Operator) – Used to access static members and constants of a class.

🔁 Inheritance & Polymorphism

  • Inheritance – Allows a class to use properties and methods of another class using extends.
  • Polymorphism – Allows different classes to define methods with the same name but different behavior.
  • Overriding – Redefining a parent class method in the child class with the same signature.
  • Overloading (PHP magic) – Achieved using __call() or __get() for dynamic method/property access (not traditional overloading like other languages).

🧩 Interfaces & Traits

  • Interface – Defines method signatures that implementing classes must define, with no method bodies.
  • Implements – A class uses implements to follow an interface contract.
  • Trait – A mechanism for code reuse in single inheritance, allowing inclusion of methods in multiple classes.
  • Use (for Trait) – Includes a trait inside a class using the use keyword.

🏗️ Access & Object Control

  • Constructor (__construct) – Automatically called when an object is created to initialize it.
  • Destructor (__destruct) – Called when an object is destroyed or script ends.
  • $this – Refers to the current object instance inside a class.
  • self – Refers to the current class (used for static access within the class).
  • parent – Refers to the parent class when overriding or calling parent methods.

🧠 Object Features & Advanced

  • Encapsulation – Hiding internal state and requiring all interaction through methods.
  • Abstraction – Hiding complex implementation details and showing only necessary features.
  • Instanceof – Used to check if an object is an instance of a specific class or interface.
  • Object Cloning (__clone) – Creates a shallow copy of an object.
  • Type Hinting – Enforces expected types for method parameters or return values.

🪄 Magic Methods

  • __construct() – The constructor method, automatically called when an object is instantiated.
  • __destruct() – Called when an object is destroyed or the script finishes execution.
  • __get() – Called when trying to access a non-existing or inaccessible property.
  • __set() – Called when trying to set a non-existing or inaccessible property.
  • __call() – Invoked when an inaccessible or non-existing method is called on the object.
  • __callStatic() – Similar to __call(), but used for static methods.
  • __isset() – Triggered when calling isset() or empty() on inaccessible or non-existing properties.
  • __unset() – Triggered when unset() is called on inaccessible or non-existing properties.
  • __toString() – Called when an object is treated as a string (e.g., echo or print).
  • __sleep() – Called before an object is serialized (via serialize()).
  • __wakeup() – Called when an object is deserialized (via unserialize()).
  • __clone() – Called when an object is cloned via the clone keyword.
  • __invoke() – Called when an object is used as a function (e.g., $obj()).

🌐 Namespaces

  • Namespace – Defines a set of related classes, interfaces, functions, or constants to avoid name collisions.
  • use – Imports classes, functions, or constants from a namespace into the current file.
  • Fully Qualified Name – The complete name of a class, including its namespace (e.g., Namespace\ClassName).
  • Global Namespace – Refers to classes, functions, or constants not defined within any namespace (e.g., \ClassName).
  • Alias – The as keyword allows giving an imported class or function a shorter name (e.g., use Some\LongNamespace\ClassName as MyClass).

🏗️ Design Patterns

  • Singleton – Ensures a class has only one instance and provides a global point of access to it.
  • Factory – Defines an interface for creating objects, but lets subclasses alter the type of objects that will be created.
  • Abstract Factory – Provides an interface for creating families of related or dependent objects without specifying their concrete classes.
  • Observer – Defines a one-to-many dependency between objects, where one object (the subject) notifies others (observers) of state changes.
  • Decorator – Allows behavior to be added to individual objects, dynamically, without affecting the behavior of other objects from the same class.
  • Strategy – Defines a family of algorithms, encapsulates each one, and makes them interchangeable.
  • Command – Encapsulates a request as an object, thereby letting users parameterize clients with queues, requests, and operations.
  • Builder – Separates the construction of a complex object from its representation, allowing the same construction process to create different representations.
  • Proxy – Provides a surrogate or placeholder for another object, controlling access to it.
  • Adapter – Converts one interface to another, so that incompatible interfaces can work together.