Magic Methods: Understanding __construct, __destruct and other core methods in PHP

PHPz
Release: 2023-06-20 06:02:01
Original
1029 people have browsed it

Magic methods: Understanding core methods such as __construct and __destruct in PHP

In the PHP language, there are some special methods called "magic methods", including __construct, __destruct, etc. These methods play an important role in object-oriented programming in PHP. This article will explain the role and practical application of these methods.

__construct method

__construct method is a very important method. It is a method that is automatically called when PHP creates a new object. In this method, we can perform some initialization work, such as setting default values ​​for the properties of the object, connecting to the database, etc. Moreover, if we do not define this method, an error will occur when the class is instantiated.

The following is a sample code:

class Person{
  public $name;
  public $age;
  
  function __construct($name, $age){
    $this->name = $name;
    $this->age = $age;
  }
  
  function showInfo(){
    echo "姓名:" . $this->name . " 年龄:" . $this->age;
  }
}

$person = new Person("张三", 20);
$person->showInfo();
Copy after login

In the above code, we define a class named Person and define a __construct method in the class to initialize the name and Age attribute. When instantiating the Person class, we pass in "Zhang San" and 20 as parameters, so that personal information can be output through the $person->showInfo() method.

__destruct method

__destruct method is a method that is automatically called when the object is destroyed. In this method, we can perform some cleanup work, such as releasing some occupied resources, etc. Similarly, if we do not define this method, an error will occur when the object is destroyed.

The following is a sample code:

class Car{
  public $brand;
  
  function __construct($brand){
    $this->brand = $brand;
  }
  
  function run(){
    echo "我开着" . $this->brand . "在马路上飞奔!";
  }
  
  function __destruct(){
    echo $this->brand . "被销毁了!";
  }
}

$car = new Car("宝马");
$car->run();
Copy after login

In the above code, we define a class named Car, and define a __destruct method in the class to output and destroy a certain vehicle information. After we instantiate the Car class, we call the $car->run() method to output the vehicle information, and output the destruction information at the end.

__call method

__call method is a method that is automatically called when a method that does not exist in the class is called. In this method, we can dynamically call a method and pass parameters. This method is very suitable for dealing with some uncertain situations during development, such as dynamically calling database operation methods.

The following is a sample code:

class Database{
  private $db;
  
  function __construct($host, $user, $password, $dbName){
    $this->db = new mysqli($host, $user, $password, $dbName);
  }
  
  function __call($method, $args){
    if(method_exists($this->db, $method)){
      return call_user_func_array([$this->db, $method], $args);
    }else{
      die("没有找到" . $method . "方法!");
    }
  }
}

$database = new Database("localhost", "root", "password", "test");
$res = $database->query("SELECT * FROM users");

while($row = $res->fetch_assoc()){
  echo $row['name'];
}
Copy after login

In the above code, we define a class named Database and define a __call method in the class for dynamic calling Methods of the mysqli class. When we instantiate the Database class and call the query method, the query() method in the mysqli class is actually called dynamically.

Conclusion

In the PHP language, magic methods provide us with many useful tools, such as the __construct method for initializing objects, the __destruct method for cleanup, and the __call method for implementation Dynamically calling methods, etc. Proficiency in these methods is very important for object-oriented programming in PHP.

The above is the detailed content of Magic Methods: Understanding __construct, __destruct and other core methods in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!