Home  >  Article  >  Backend Development  >  What is the function of php magic method

What is the function of php magic method

PHPz
PHPzOriginal
2023-04-24 10:53:37835browse

PHP is an object-oriented programming language, and its object-oriented programming features are very powerful. In PHP, there are many functions and methods for operating objects, the most special and commonly used of which are magic methods.

Magic method, also called magic method, is a set of special methods defined in a class to respond to some specific events in the class. These events can be instantiating an object, calling a method that does not exist in the class, reading a property that does not exist in the class, etc. There are many built-in magic methods in PHP, usually starting and ending with two underscores, such as __construct(), __destruct(), __call(), __get(), __set(), etc. Magic methods can provide a class with some features that are not available in regular methods, making the class more flexible and easier to use.

Let’s introduce one by one some magic methods commonly used in PHP.

  1. __construct()

__construct() method is a class construction method, which is used to automatically call when instantiating an object to complete the initialization of object properties. It usually accepts some parameters, which are passed when instantiating the object. For example:

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

$p = new Person('Tom', 18);
echo $p->name;  // 输出 Tom
  1. __destruct()

__destruct() method is a destructor method of a class, which is used to automatically call when the object is destroyed to complete some cleanup work. For example, close the database connection, release resources, etc. It does not accept any parameters. For example:

class Database {
    public function __construct() {
        // 连接数据库...
    }
    public function __destruct() {
        // 关闭数据库连接...
    }
}

$db = new Database();
// 使用数据库...
unset($db);  // 销毁对象
  1. __call()

__call() method is used to automatically call when the object calls a method that does not exist. It will accept two parameters: method name and method parameters. You can use this feature to dynamically call methods. For example:

class Person {
    public function sayHello($name) {
        echo 'Hello, ' . $name . '!';
    }
    public function __call($method, $args) {
        if ($method == 'sayHi') {
            echo 'Hi!';
        }
    }
}

$p = new Person();
$p->sayHello('Tom');  // 输出 Hello, Tom!
$p->sayHi();  // 输出 Hi!
  1. __get()

__get() method is used to automatically call when reading a non-existent attribute. It will accept one parameter: attribute name . You can use this feature to implement lazy loading or dynamic calculation of properties. For example:

class Person {
    private $name;
    private $age;
    public function __get($name) {
        if ($name == 'ageInDays') {
            return $this->age * 365;
        }
    }
    public function __construct($name, $age) {
        $this->name = $name;
        $this->age = $age;
    }
}

$p = new Person('Tom', 18);
echo $p->ageInDays;  // 输出 6570
  1. __set()

__set() method is used to automatically call when setting a non-existent attribute. It will accept two parameters: attribute name and attribute value. You can use this feature to implement attribute restrictions, filtering, or dynamic calculations. For example:

class Person {
    private $name;
    private $age;
    public function __set($name, $value) {
        if ($name == 'age' && $value < 0) {
            throw new Exception(&#39;Age cannot be negative.&#39;);
        }
        $this->$name = $value;
    }
}

$p = new Person();
$p->name = 'Tom';
$p->age = -18;  // 抛出异常

In PHP, magic methods are very important and widely used. Using magic methods can better achieve object encapsulation and efficient programming, and can also add more readability and reusability to the code. In actual development, we should make reasonable use of magic methods and give full play to its role.

The above is the detailed content of What is the function of php magic method. 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