Home  >  Article  >  Backend Development  >  PHP Object-Oriented Programming: Practical Exercise

PHP Object-Oriented Programming: Practical Exercise

WBOY
WBOYOriginal
2024-05-09 21:57:01450browse

PHP Object-oriented programming (OOP) is a programming paradigm that simulates real entities. The core concepts of OOP include: Classes and Objects: A class defines a blueprint for an object, and an object is an instance of a class. Encapsulation: Object properties and methods are isolated from other code. Inheritance: Subclasses can inherit the properties and methods of the parent class. Polymorphism: A method with the same name exhibits different behavior at runtime depending on the type of object.

PHP Object-Oriented Programming: Practical Exercise

PHP Object-Oriented Programming: Practical Exercise

Introduction

Object-Oriented Programming (OOP) is a widely adopted programming paradigm in modern software development that is based on the idea of ​​modeling real-world entities. In this article, we will explore OOP in PHP in depth and demonstrate its power through practical examples.

Classes and Objects

In OOP, a class is the blueprint of an object, which defines its data and behavior. An object is an instance of a class that has its own data and behavior, but the behavior is governed by the definition of the class to which it belongs.

Code sample:

class Car {
  private $model;
  private $make;

  public function __construct($model, $make) {
    $this->model = $model;
    $this->make = $make;
  }

  public function getDetails() {
    return "Model: {$this->model}, Make: {$this->make}";
  }
}

$car = new Car('Camry', 'Toyota');
echo $car->getDetails();

Encapsulation

Encapsulation is one of the core principles of OOP, which describes how to The ability of an object's properties and methods to be isolated from other code.

Code Example:

class User {
  private $username;
  private $password;

  public function setUsername($username) {
    // 验证用户名并设置
    $this->username = $username;
  }

  public function getUsername() {
    return $this->username;
  }
}

$user = new User();
$user->setUsername('admin');
echo $user->getUsername();

Inheritance

Inheritance allows the creation of a new class ( Subclass), the new class will inherit the data and behavior of the parent class, and can also define its own specific properties and methods.

Code example:

class Vehicle {
  private $make;
  private $model;

  public function __construct($make, $model) {
    $this->make = $make;
    $this->model = $model;
  }

  public function getDetails() {
    return "Make: {$this->make}, Model: {$this->model}";
  }
}

class Car extends Vehicle {
  private $numWheels;

  public function __construct($make, $model, $numWheels) {
    parent::__construct($make, $model);
    $this->numWheels = $numWheels;
  }

  public function getDetails() {
    return parent::getDetails() . ", Num Wheels: {$this->numWheels}";
  }
}

$car = new Car('Toyota', 'Camry', 4);
echo $car->getDetails();

Polymorphism

Polymorphism allows parent and child class methods to have the same name, but exhibits different behavior at runtime depending on the actual type of the object.

Code example:

class Polygon {
  abstract public function getArea();
}

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

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

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

class Circle extends Polygon {
  private $radius;

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

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

function calculateTotalArea($polygons) {
  $totalArea = 0;
  foreach ($polygons as $polygon) {
    $totalArea += $polygon->getArea();
  }
  return $totalArea;
}

$polygons = [
  new Rectangle(5, 10),
  new Circle(5),
];

echo calculateTotalArea($polygons);

The above is the detailed content of PHP Object-Oriented Programming: Practical Exercise. 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