Home > Backend Development > PHP Tutorial > Advanced features of encapsulation in PHP

Advanced features of encapsulation in PHP

PHPz
Release: 2023-10-12 11:22:01
Original
1477 people have browsed it

Advanced features of encapsulation in PHP

Advanced features of encapsulation in PHP require specific code examples

Encapsulation is a very important concept in object-oriented programming. It encapsulates data and behavior in Inside an object, thus achieving data hiding and protection. As an object-oriented language, PHP also provides a wealth of advanced encapsulation features. This article will introduce these features through specific code examples.

  1. Access Control

Access control is the core of encapsulation, which can limit the access rights of properties and methods. PHP provides three different access control modifiers: public, protected and private. The following is an example:

class Person {
  public $name;  // 公共属性
  protected $age;  // 受保护的属性
  private $email;  // 私有属性

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

  public function getAge() {
    return $this->age;  // 只能在类内部访问
  }
}

$person = new Person("John", 25, "john@example.com");
echo $person->name;  // 可以直接访问
echo $person->age;  // 报错,受保护的属性不能在外部访问
echo $person->email;  // 报错,私有属性不能在外部访问
echo $person->getAge();  // 可以通过公共方法访问受保护的属性
Copy after login
  1. Encapsulation of Inheritance

Encapsulated inheritance is to reuse the properties and methods of the parent class by inheriting the parent class, and You can add your own unique properties and methods. Here is an example:

class Animal {
  private $name;

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

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

class Dog extends Animal {
  private $breed;

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

  public function getBreed() {
    return $this->breed;
  }
}

$dog = new Dog("Max", "Golden Retriever");
echo $dog->getName();  // 可以调用父类的方法
echo $dog->getBreed();  // 可以调用子类的方法
Copy after login
  1. Encapsulation of Polymorphism

Encapsulated polymorphism is achieved by aggregating similar objects with different implementations together. Thus polymorphism is achieved. The interface in PHP can implement encapsulated polymorphism. The following is an example:

interface Shape {
  public function calculateArea();
}

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

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

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

class Circle implements Shape {
  private $radius;

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

  public function calculateArea() {
    return 3.14 * $this->radius * $this->radius;
  }
}

$rectangle = new Rectangle(5, 10);
$circle = new Circle(7);

echo $rectangle->calculateArea();  // 输出50
echo $circle->calculateArea();  // 输出153.86
Copy after login

Summary:

PHP provides advanced features such as access control, encapsulated inheritance and encapsulated polymorphism, which can help us achieve encapsulation and protect object data , while providing good code reuse and scalability. Mastering these features can improve the maintainability and security of the code and make software development more efficient.

The above is the detailed content of Advanced features of encapsulation in PHP. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template