PHP Fundamentals

Learn PHP programming from scratch, including variables, functions, arrays, and object-oriented programming.

beginner Backend Development 6 hours

Chapter 11: Object-Oriented Programming

Chapter 11 of 15

Chapter 11: Object-Oriented Programming

11.1 Classes and Objects

class User {
    public $name;
    private $email;
    
    public function __construct($name, $email) {
        $this->name = $name;
        $this->email = $email;
    }
    
    public function getEmail() {
        return $this->email;
    }
}

$user = new User("John", "john@example.com");

11.2 Properties and Methods

Classes contain properties (variables) and methods (functions).

11.3 Inheritance

class Admin extends User {
    public function deleteUser($id) {
        // Admin-specific functionality
    }
}