Home  >  Article  >  Backend Development  >  In-depth understanding of PHP object-oriented programming: extension and customization of object-oriented programming

In-depth understanding of PHP object-oriented programming: extension and customization of object-oriented programming

王林
王林Original
2024-06-04 11:26:04282browse

PHP object-oriented programming can be extended through extensions and custom classes. An extended class inherits the properties and methods of the parent class and can add new properties and methods; a custom class implements specific functions by implementing interface methods. In the actual case, by extending the abstract class Shape, concrete shapes such as Circle and Rectangle are created, and the area can be calculated dynamically.

In-depth understanding of PHP object-oriented programming: extension and customization of object-oriented programming

PHP Object-Oriented Programming: Extensions and Customization

Object-oriented programming (OOP) allows you to create reusable, maintainable code. In PHP, OOP can be further extended by extending and customizing existing classes.

Extended classes

Use the extends keyword to extend a class. The extended class inherits all properties and methods of the parent class and can add new properties and methods.

class BaseClass {
    protected $name;

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

    public function getName() {
        return $this->name;
    }
}

class ExtendedClass extends BaseClass {
    private $age;

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

    public function getAge() {
        return $this->age;
    }
}

Customized class

Use the implements keyword to customize a class so that it implements one or more interfaces. An interface defines a set of methods that the class must implement.

interface MyInterface {
    public function doSomething();
}

class MyClass implements MyInterface {
    public function doSomething() {
        // 具体实现
    }
}

Practical case

Consider an abstract class Shape, which defines a getArea() method. We extend this class to create concrete shapes such as Circle and Rectangle.

abstract class Shape {
    protected $color;

    public function __construct($color) {
        $this->color = $color;
    }

    abstract public function getArea();
}

class Circle extends Shape {
    private $radius;

    public function __construct($color, $radius) {
        parent::__construct($color);
        $this->radius = $radius;
    }

    public function getArea() {
        return pi() * $this->radius ** 2;
    }
}

class Rectangle extends Shape {
    private $width;
    private $height;

    public function __construct($color, $width, $height) {
        parent::__construct($color);
        $this->width = $width;
        $this->height = $height;
    }

    public function getArea() {
        return $this->width * $this->height;
    }
}

We can dynamically calculate the area by creating Circle and Rectangle objects and accessing their respective getArea() methods.

The above is the detailed content of In-depth understanding of PHP object-oriented programming: extension and customization of object-oriented programming. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn