Parsing class attributes and methods in PHP object-oriented programming

王林
Release: 2023-08-12 13:08:01
Original
1644 people have browsed it

Parsing class attributes and methods in PHP object-oriented programming

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.

  1. Public properties (public): Public properties can be directly accessed inside and outside the class. Examples are as follows:
class Car {
    public $color = "red";
    public $brand = "Toyota";
}

$myCar = new Car();
echo $myCar->color;  // 输出:red
echo $myCar->brand;  // 输出:Toyota
Copy after login
  1. Protected properties (protected): Protected properties can only be accessed within the class and its subclasses. Examples are as follows:
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;       // 错误:不能直接访问受保护属性
Copy after login
  1. Private properties (private): Private properties can only be accessed within the class and cannot be accessed outside the class or in subclasses. Examples are as follows:
class Car {
    private $color = "red";
    private $brand = "Toyota";
    
    public function getColor() {
        return $this->color;
    }
}

$myCar = new Car();
echo $myCar->getColor();  // 输出:red
echo $myCar->brand;       // 错误:不能直接访问私有属性
Copy after login

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.

  1. Public method (public): Public methods can be called directly inside and outside the class. Examples are as follows:
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
Copy after login
  1. Protected method (protected): Protected methods can only be called within the class and its subclasses. Examples are as follows:
class Shape {
    protected function calculateArea() {
        // 计算面积的具体实现
    }
}

class Circle extends Shape {
    public function getArea() {
        return $this->calculateArea();
    }
}

$myCircle = new Circle();
echo $myCircle->calculateArea();  // 错误:不能直接调用受保护方法
echo $myCircle->getArea();        // 正确:通过公共方法调用受保护方法
Copy after login
  1. Private method (private): Private methods can only be called inside the class and cannot be called outside the class or in subclasses. Examples are as follows:
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);        // 错误:不能直接调用私有方法
Copy after login

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!

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