How do I create custom classes

Learn how to create custom classes in PHP. This guide provides a detailed example to help you understand the fundamentals of object-oriented programming in PHP.

PHP classes, custom classes, object-oriented programming, PHP OOP


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;
    }
}

$person = new Person("John Doe", 30);
echo "Name: " . $person->getName() . "
"; echo "Age: " . $person->getAge();

PHP classes custom classes object-oriented programming PHP OOP