DEV Community

Zouhair Ghaidoud
Zouhair Ghaidoud

Posted on

Object-oriented programming in PHP

Object-oriented programming (OOP) in PHP is a powerful way to structure and manage your code. Here’s a basic introduction to help you get started:

1. Classes and Objects

Classes are blueprints for objects. They define properties (variables) and methods (functions) that the objects created from the class will have.

<?php
class Car {
    public $color; // Property
    public $model;

    // Constructor
    public function __construct($color, $model) {
        $this->color = $color;
        $this->model = $model;
    }

    // Method
    public function getDetails() {
        return "This is a $this->color $this->model.";
    }
}

// Creating an object
$myCar = new Car("red", "Toyota");

// Accessing a method
echo $myCar->getDetails();
?>
Enter fullscreen mode Exit fullscreen mode

2. Properties and Methods

Properties: Variables that belong to a class.
Methods: Functions that belong to a class.

<?php
class Person {
    public $name; // Property
    private $age; // Private property

    // Method
    public function setName($name) {
        $this->name = $name;
    }

    // Private Method
    private function setAge($age) {
        $this->age = $age;
    }
}

$person = new Person();
$person->setName("John Doe");
echo $person->name; // Outputs: John Doe
?>
Enter fullscreen mode Exit fullscreen mode

3. Visibility

public: Accessible from anywhere.
private: Accessible only within the class.
protected: Accessible within the class and by inheriting classes.

<?php
class Example {
    public $publicVar = "I am public";
    private $privateVar = "I am private";
    protected $protectedVar = "I am protected";

    public function showVariables() {
        echo $this->publicVar;
        echo $this->privateVar;
        echo $this->protectedVar;
    }
}

$example = new Example();
echo $example->publicVar; // Works
// echo $example->privateVar; // Fatal error
// echo $example->protectedVar; // Fatal error
?>
Enter fullscreen mode Exit fullscreen mode

4. Inheritance

Inheritance allows a class to use the properties and methods of another class.

<?php
class Animal {
    public $name;

    public function speak() {
        echo "Animal sound";
    }
}

class Dog extends Animal {
    public function speak() {
        echo "Woof! Woof!";
    }
}

$dog = new Dog();
$dog->speak(); // Outputs: Woof! Woof!
?>
Enter fullscreen mode Exit fullscreen mode

5. Interfaces

Interfaces allow you to define methods that must be implemented in any class that implements the interface.

<?php
interface Shape {
    public function area();
}

class Circle implements Shape {
    private $radius;

    public function __construct($radius) {
        $this->radius = $radius;
    }

    public function area() {
        return pi() * $this->radius * $this->radius;
    }
}

$circle = new Circle(5);
echo $circle->area(); // Outputs the area of the circle
?>
Enter fullscreen mode Exit fullscreen mode

6. Abstract Classes

Abstract classes cannot be instantiated and are meant to be extended by other classes. They can have both abstract and concrete methods.

<?php
abstract class Vehicle {
    abstract public function startEngine();

    public function honk() {
        echo "Honk! Honk!";
    }
}

class Car extends Vehicle {
    public function startEngine() {
        echo "Car engine started";
    }
}

$car = new Car();
$car->startEngine(); // Outputs: Car engine started
$car->honk(); // Outputs: Honk! Honk!
?>
Enter fullscreen mode Exit fullscreen mode

7. Traits

Traits are a mechanism for code reuse in single inheritance languages like PHP. They allow you to include methods in multiple classes.

<?php
trait Logger {
    public function log($message) {
        echo "Log: $message";
    }
}

class Application {
    use Logger;
}

$app = new Application();
$app->log("Application started"); // Outputs: Log: Application started
?>
Enter fullscreen mode Exit fullscreen mode

8. Namespaces

Namespaces are a way to encapsulate items to avoid name conflicts.

<?php
namespace MyApp;

class User {
    public function __construct() {
        echo "User class from MyApp namespace";
    }
}

$user = new \MyApp\User();
?>
Enter fullscreen mode Exit fullscreen mode

Practice and Resources
Practice: Write small programs using these concepts to get comfortable with OOP in PHP.
Resources:

By understanding and practicing these concepts, you’ll be well on your way to mastering OOP in PHP.

Top comments (0)