PHP Basics
Functions in PHP
Working with Forms
Working with Files
Working with Databases
Advanced PHP Techniques

Object-Oriented Programming (OOP) in PHP involves using objects and classes to create modular, reusable, and maintainable code. In OOP, properties and methods are fundamental components of classes. Let’s dive into these concepts in detail:

Classes and Objects

  • Class: A blueprint for creating objects. It defines properties and methods.
  • Object: An instance of a class. It is created using the new keyword.

Properties

Properties in PHP are variables that belong to a class. They are used to store data related to objects.

Defining Properties

Properties are defined inside a class using visibility keywords:

  • public: The property can be accessed from anywhere.
  • protected: The property can be accessed within the class itself and by inheriting classes.
  • private: The property can only be accessed within the class itself.

Example:

				
					class Car {
    public $color;
    protected $model;
    private $engineNumber;
    
    public function __construct($color, $model, $engineNumber) {
        $this->color = $color;
        $this->model = $model;
        $this->engineNumber = $engineNumber;
    }
}

				
			

Accessing Properties

  • Public Properties: Can be accessed directly from outside the class.
  • Protected and Private Properties: Require getter and setter methods for access.

Example:

				
					$car = new Car('Red', 'Tesla', '1234');
echo $car->color; // Accessing public property

class Car {
    //...
    public function getModel() {
        return $this->model;
    }
    
    public function setModel($model) {
        $this->model = $model;
    }
}

$car->setModel('Tesla');
echo $car->getModel(); // Accessing protected property via method

				
			

Methods

Methods in PHP are functions defined inside a class. They define the behavior of an object.

Defining Methods

Methods can also have visibility keywords: public, protected, or private.

Example:

				
					class Car {
    public $color;
    
    public function __construct($color) {
        $this->color = $color;
    }
    
    public function drive() {
        echo "The car is driving";
    }
    
    protected function startEngine() {
        echo "Engine started";
    }
    
    private function checkEngine() {
        echo "Engine checked";
    }
}

				
			

Calling Methods

  • Public Methods: Can be called from outside the class.
  • Protected and Private Methods: Can only be called from within the class or inheriting classes.

Example:

				
					$car = new Car('Red');
$car->drive(); // Calling public method

class SportCar extends Car {
    public function startSportCar() {
        $this->startEngine(); // Calling protected method from inheriting class
    }
}

$sportCar = new SportCar('Blue');
$sportCar->startSportCar(); // Works fine

// $sportCar->checkEngine(); // Error: Cannot access private method

				
			

Static Properties and Methods

Static properties and methods belong to the class itself rather than to any particular object instance. They are accessed using the self keyword and the scope resolution operator ::.

Example:

				
					class Car {
    public static $count = 0;
    
    public static function incrementCount() {
        self::$count++;
    }
    
    public static function getCount() {
        return self::$count;
    }
}

Car::incrementCount();
Car::incrementCount();
echo Car::getCount(); // Output: 2

				
			

Constructors and Destructors

  • Constructor: A special method called when an object is instantiated. It is defined using __construct.
  • Destructor: A special method called when an object is destroyed. It is defined using __destruct.

Example:

				
					class Car {
    public function __construct() {
        echo "Car created";
    }
    
    public function __destruct() {
        echo "Car destroyed";
    }
}

$car = new Car(); // Output: Car created
unset($car); // Output: Car destroyed

				
			

Inheritance

Inheritance allows a class to inherit properties and methods from another class using the ‘extends‘ keyword.

Example:

				
					class Vehicle {
    public $brand;
    
    public function __construct($brand) {
        $this->brand = $brand;
    }
    
    public function honk() {
        echo "Honk!";
    }
}

class Car extends Vehicle {
    public function drive() {
        echo "Driving a " . $this->brand;
    }
}

$car = new Car('Toyota');
$car->honk(); // Output: Honk!
$car->drive(); // Output: Driving a Toyota

				
			

Visibility Keywords in Detail

  • Public: Accessible from anywhere.
  • Protected: Accessible within the class and by inheriting classes.
  • Private: Accessible only within the class.

Example of a Full Class

				
					class Car {
    public $color;
    protected $model;
    private $engineNumber;
    
    public function __construct($color, $model, $engineNumber) {
        $this->color = $color;
        $this->model = $model;
        $this->engineNumber = $engineNumber;
    }
    
    public function drive() {
        echo "The car is driving";
    }
    
    protected function startEngine() {
        echo "Engine started";
    }
    
    private function checkEngine() {
        echo "Engine checked";
    }
    
    public function getModel() {
        return $this->model;
    }
    
    public function setModel($model) {
        $this->model = $model;
    }
}

$car = new Car('Red', 'Tesla', '1234');
echo $car->color; // Output: Red
$car->drive(); // Output: The car is driving
$car->setModel('BMW');
echo $car->getModel(); // Output: BMW

				
			

Understanding these concepts is crucial for effectively using OOP in PHP, allowing for better code organization, reusability, and maintainability.

Scroll to Top