OOP

7 questions found

What are the four core principles of object oriented programming, and how does PHP support each of them?

Beginner
The four core principles are encapsulation, which means bundling data and the methods that operate on it together while controlling access using visibility modifiers, inheritance, which lets a class inherit properties and methods from a parent class, polymorphism, which lets different classes be used interchangeably through a shared interface or parent class, and abstraction, which means exposing only essential details while hiding complex implementation, and PHP fully supports all four of these principles through its class, interface, and visibility keyword syntax.
class Animal {
    protected $name;
    public function makeSound() { return 'Some sound'; }
}
class Dog extends Animal {
    public function makeSound() { return 'Woof'; }
}
Real-world example A pet management application defines a base Animal class with shared properties and behavior, then creates specific Dog and Cat subclasses that each override the makeSound method appropriately, demonstrating inheritance and polymorphism working together.

Common follow-ups: How do these four principles work together in a typical well designed class hierarchy?;Which of these four principles is generally considered the most fundamental?

Interfaces & Abstract Classes;Design Patterns in PHP

What are the three visibility modifiers in PHP, public, protected, and private, and how do they control access to a class's properties and methods?

Beginner
The public modifier allows a property or method to be accessed from anywhere, including outside the class entirely, protected restricts access to the class itself and any classes that extend it, and private restricts access exclusively to the exact class where it is defined, not even allowing access from a subclass, and choosing the appropriate visibility for each member is an important part of properly encapsulating a class's internal implementation details.
class BankAccount {
    private $balance;
    protected $accountType;
    public $accountNumber;
}
Real-world example A BankAccount class marks its balance property as private to prevent any external code from directly modifying it without going through proper validated methods, while marking accountNumber as public since it is safe and expected to be freely read by other parts of the application.

Common follow-ups: Why might a property be marked protected rather than private if a subclass needs access to it?;What happens if you try to access a private property from outside its defining class?

Interfaces & Abstract Classes;Security

How does inheritance work in PHP using the extends keyword, and how do you call a parent class's constructor or method from within a child class?

Intermediate
The extends keyword lets a child class inherit the properties and methods of a parent class, automatically gaining access to everything the parent defines as public or protected, and you can explicitly call a parent class's constructor or a specific overridden method using the parent keyword followed by the scope resolution operator, which is commonly used when a child class needs to extend rather than completely replace its parent's behavior.
class Vehicle {
    public function __construct(protected string $brand) {}
}
class Car extends Vehicle {
    public function __construct(string $brand, private int $doors) {
        parent::__construct($brand);
    }
}
Real-world example A Car class extends a base Vehicle class, calling the parent constructor to properly initialize the shared brand property while also handling its own additional doors property specific to cars.

Common follow-ups: Can a class extend more than one parent class at the same time in PHP?;What is the difference between overriding a method entirely versus extending its behavior using parent?

Interfaces & Abstract Classes;Traits

What is the difference between a static property or method and an instance property or method in PHP?

Intermediate
A static property or method belongs to the class itself rather than to any specific instance, meaning it is accessed using the class name and scope resolution operator rather than through an object instance, and its value is shared across the entire application rather than being unique to each individual object, while an instance property or method requires creating an actual object and each instance maintains its own independent copy of instance property values.
class Counter {
    private static int $count = 0;
    public static function increment(): void {
        self::$count++;
    }
}
Counter::increment();
Real-world example A utility class tracking how many database queries have been executed throughout the entire request uses a static property, since that count needs to be shared globally rather than tracked separately per individual object instance.

Common follow-ups: When is it appropriate to use a static method instead of an instance method?;Can a static method access instance properties or call instance methods?

Functions & Scope;Design Patterns in PHP

How does the readonly property modifier introduced in PHP 8.1 help enforce immutability for specific class properties?

Intermediate
The readonly modifier lets you declare a property that can only be assigned a value once, typically within the constructor, and any subsequent attempt to modify that property from anywhere, including from within the class itself, throws an error, which is valuable for creating immutable value objects where a property's value should genuinely never change after the object is first created, catching accidental mutation bugs at the point they occur rather than allowing them to silently happen.
class Point {
    public function __construct(public readonly float $x, public readonly float $y) {}
}
$point = new Point(3.0, 4.0);
// $point->x = 5.0; // Throws an Error
Real-world example A coordinate system uses readonly properties for its Point class, guaranteeing that once a point is created, its x and y values can never be accidentally modified elsewhere in the codebase, providing strong confidence in the object's immutability.

Common follow-ups: Can a readonly property be modified through a method defined within the same class after construction?;How do readonly properties compare to using getter methods without any corresponding setter methods?

PHP 8 Features (Attributes Enums Match Nullsafe Operator);Type Declarations & Strict Types

How does PHP handle multiple inheritance limitations, and how do traits provide a mechanism for sharing reusable code across multiple unrelated class hierarchies?

Advanced
PHP classes can only extend a single parent class, meaning true multiple inheritance is not supported, but traits provide a mechanism for horizontally sharing reusable methods across multiple, otherwise unrelated classes, letting you compose shared behavior into a class using the use keyword within the class body, effectively providing many of the code reuse benefits of multiple inheritance without the complexity and ambiguity issues that true multiple inheritance can introduce.
trait Loggable {
    public function log($message) { echo "[LOG] $message"; }
}
class OrderService {
    use Loggable;
}
class PaymentService {
    use Loggable;
}
Real-world example Two otherwise unrelated classes, OrderService and PaymentService, both incorporate the exact same logging functionality by using a shared Loggable trait, avoiding code duplication despite neither class extending a common parent class.

Common follow-ups: What happens if a class uses two different traits that both define a method with the same name?;How do traits differ from interfaces in terms of what they actually provide to a class?

Traits;Interfaces & Abstract Classes

How does late static binding, using the static keyword instead of self, allow inherited static methods to correctly reference the actual calling child class rather than the parent class where the method was originally defined?

Advanced
When a static method uses self to reference the current class, that reference always resolves to the class where the method was actually defined, even when called through a child class, whereas using the static keyword instead resolves to whichever specific class was actually used to make the call, a behavior called late static binding, which is essential for patterns like a base class providing a shared static factory method that should correctly return an instance of whichever specific subclass actually called it.
class Model {
    public static function create(): static {
        return new static();
    }
}
class User extends Model {
}
$user = User::create();
echo get_class($user);
Real-world example A base Model class provides a shared static create factory method using late static binding, ensuring that calling User::create actually returns a genuine User instance rather than incorrectly always returning a plain Model instance regardless of which specific subclass was called.

Common follow-ups: What is a concrete example where using self instead of static would produce an incorrect, unexpected result?;How does late static binding relate to the way Eloquent's static query methods work correctly across different model subclasses?

Eloquent ORM & Doctrine ORM;Design Patterns in PHP