Object-Oriented Programming in PHP

Object-Oriented Programming in PHP

Object-oriented programming (OOP) in PHP is used to create reusable objects that can be used in a program. OOP involves creating classes, which are templates for objects, and then creating objects from those classes.

For example, a simple class that defines a Person object:

class Person {
    public $name;
    public $age;

    public function __construct($name, $age) {
        $this->name = $name;
        $this->age = $age;

In this example, the class Person is defined with two properties, $name, and $age. The constructor is also defined which takes two parameters, $name, and $age, and assigns them to the object’s properties. The output of this class when an object is created would be an instance of the Person class with the properties $name and $age set to the values passed in the constructor.

Leave a Reply