Analysis of class attributes and methods in PHP object-oriented programming
PHP is a scripting language widely used in Web development. It supports object-oriented programming (OOP). ) characteristics. In PHP, a class is a blueprint or template used to create objects, and properties and methods are the core part of the class. This article will provide an in-depth analysis of class attributes and methods in PHP object-oriented programming, and deepen understanding through code examples.
1. Class attributes
Class attributes refer to variables used to describe unique data of a class. They can store the state and characteristics of objects. In PHP, class attributes have three access modifiers: public, protected, and private.
class Car { public $color = "red"; public $brand = "Toyota"; } $myCar = new Car(); echo $myCar->color; // 输出:red echo $myCar->brand; // 输出:Toyota
class Car { protected $color = "red"; protected $brand = "Toyota"; } class SportsCar extends Car { public function getColor() { return $this->color; } } $sportsCar = new SportsCar(); echo $sportsCar->getColor(); // 输出:red echo $sportsCar->brand; // 错误:不能直接访问受保护属性
class Car { private $color = "red"; private $brand = "Toyota"; public function getColor() { return $this->color; } } $myCar = new Car(); echo $myCar->getColor(); // 输出:red echo $myCar->brand; // 错误:不能直接访问私有属性
2. Class methods
Class methods refer to functions defined in a class and are used to operate the properties of the class or complete specific tasks. Like properties, class methods also have three access modifiers: public, protected, and private.
class Circle { public $radius; public function getArea() { return 3.14 * $this->radius * $this->radius; } } $myCircle = new Circle(); $myCircle->radius = 5; echo $myCircle->getArea(); // 输出:78.5
class Shape { protected function calculateArea() { // 计算面积的具体实现 } } class Circle extends Shape { public function getArea() { return $this->calculateArea(); } } $myCircle = new Circle(); echo $myCircle->calculateArea(); // 错误:不能直接调用受保护方法 echo $myCircle->getArea(); // 正确:通过公共方法调用受保护方法
class MathUtil { private function add($a, $b) { return $a + $b; } public function calculate($a, $b) { return $this->add($a, $b); } } $mathUtil = new MathUtil(); echo $mathUtil->calculate(2, 3); // 输出:5 echo $mathUtil->add(2, 3); // 错误:不能直接调用私有方法
Summary:
Class attributes and methods are an important part of object-oriented programming in PHP. By defining class properties, we can save the state and characteristics of an object; and by defining class methods, we can manipulate these properties or complete specific tasks. Understanding the access modifiers of class properties and methods can help us flexibly control the access permissions of properties and methods. I hope that through the analysis and sample code of this article, readers will have a deeper understanding of class attributes and methods in PHP object-oriented programming.
The above is the detailed content of Parsing class attributes and methods in PHP object-oriented programming. For more information, please follow other related articles on the PHP Chinese website!