Object-Oriented Programming in PHP

 Object-Oriented Programming in PHP


PHP supports object-oriented programming (OOP) concepts such as encapsulation, inheritance, and polymorphism. OOP can help make code more modular and easier to maintain.

Example:


php


// define a class

class Person {

    private $name;

    private $age;

    

    public function __construct($name, $age) {

        $this->name = $name;

        $this->age = $age;

    }

    

    public function getName() {

        return $this->name;

    }

    

    public function getAge() {

        return $this->age;

    }

}


// create an instance of the class

$person = new Person("John", 30);

echo $person->getName();

echo $person->getAge();

No comments:

Post a Comment

The Importance of Cybersecurity in the Digital Age

 The Importance of Cybersecurity in the Digital Age Introduction: In today's digital age, where technology is deeply intertwined with ev...