๐Ÿ“– PHP Object Oriented Programming (OOP)

What Are the Core Concepts of OOP?

Abstraction
Hides internal implementation details and exposes only essential features. In PHP, abstraction is used to define base classes with generalized methods that are implemented more specifically in child classes.
Inheritance
Allows one class (a child) to reuse code from another (a parent). This supports code reuse and logical hierarchy. A child class inherits public and protected properties and methods from its parent.
Encapsulation
Protects internal object state by restricting direct access to its properties. PHP uses visibility keywords like private, protected, and public to control access to class members.
Polymorphism
Enables different classes to be used interchangeably if they share a common interface or inherit from a common base. This lets you write flexible code that can handle objects with different internal structures through a consistent external interface.

Using Classes and Objects in PHP

Object-oriented syntax introduces a new structure to programming that can feel abstract at first. Unlike procedural code, which flows top to bottom, object-oriented code organizes logic into reusable units โ€” classes โ€” that interact through methods and properties. This section walks through the basic syntax and important keywords used in PHP classes.

PHP Class Example

The following is a simple class definition that stores user data and outputs a personalized greeting:

<?php
class User {
  public $name = "Stephen";
  public $favColor = "blue";

  public function greeting() {
    return "Greetings " . $this->name . ". Is your favorite color " . $this->favColor . " today?";
  }
}

$greeting1 = new User;
echo $greeting1->greeting();

When this code runs, the output will be:

Greetings Stephen. Is your favorite color blue today?

Key Syntax Elements

-> (Object Operator)
Used to access an object's properties and methods. This operator connects the object name to its class-defined members.
$this Keyword
Refers to the current object inside a class. It allows class methods to access or modify the instance's own properties, regardless of what variable name was used to instantiate the object.

This foundational syntax allows objects in PHP to encapsulate behavior and maintain internal state โ€” an essential pattern in modern application development.

Naming Conventions in OOP

Consistent naming helps you write readable, team-friendly code โ€” and it's essential when working with object-oriented PHP. These conventions match professional standards used in PHP frameworks and real-world projects.

ClassName
Use StudlyCaps (also called PascalCase). Each word starts with a capital letter. Examples: UserModel, ProfileController, EmailService.
methodName()
Use camelCase. The first word is lowercase, and each following word is capitalized. Examples: createUser(), getUserById().
$variableName
Use camelCase for variables and object properties. Examples: $userId, $dbConnection.
snake_case
Use snake_case only for file names or function names outside of classes (e.g., legacy scripts or helper functions). Example: form_helpers.php, validate_email().
kebab-case
Never use kebab-case (dashes) in PHP code โ€” it's not valid syntax. However, itโ€™s fine for CSS classes or file names in URLs. Example: user-profile.css.

๐Ÿ’ก Tip: Use camelCase for all methods and properties inside classes, and StudlyCaps for class names. This matches professional PHP standards like PSR-12 and makes your code easier to work with.

OOP in Real Projects

Once you're comfortable creating classes and objects, the next step is applying them to structure your PHP applications. Instead of scattering functions across different files, object-oriented design encourages grouping related logic into reusable classes. This improves maintainability and reflects how modern PHP frameworks โ€” like Laravel โ€” organize code using the MVC (Modelโ€“Viewโ€“Controller) pattern.

Example: A Basic User Controller

Consider a registration system. Rather than handling everything procedurally, we can define a UserController class to group all user-related functionality in one place:

// controllers/UserController.php
class UserController {
    public function showRegistrationForm() {
        include 'views/register.php';
    }

    public function registerUser() {
        // Logic to validate form data and create a user record
    }
}

This controller defines two methods: one to display the registration form, and another to process the submitted user data. Each method encapsulates a specific behavior, making the code easier to read, reuse, and extend.

Using the Controller

To activate your controller, you create an instance of the class and call its method like this:

// register.php
require_once 'controllers/UserController.php';

$controller = new UserController();
$controller->showRegistrationForm();

Here's what's happening in these lines:

require_once
Loads the controller file so the class definition is available. This prevents the file from being loaded more than once if included elsewhere.
new UserController()
Creates a new instance (object) of the UserController class. This object now has access to all methods defined in the class.
$controller->showRegistrationForm()
Uses the object operator (->) to call the showRegistrationForm() method from the instantiated controller. This method then loads the registration form view.

This pattern โ€” instantiate, then call โ€” is used frequently in PHP applications that follow object-oriented design. It's the foundation for how controller logic interacts with views and models in structured web frameworks.

Last updated: August 6, 2025 at 4:06 PM