Home  >  Article  >  Backend Development  >  The role of php constructor

The role of php constructor

angryTom
angryTomOriginal
2019-08-23 11:55:433104browse

The role of php constructor

What is the function of the constructor? In order to give you a better understanding, let me start with the example of a factory:

Recommended tutorial: PHP video tutorial

We know that in a factory, if you want to produce products with the same requirements, you need CAD molds. If extended to our PHP, we can understand that the class is the CAD mold, which determines the shape of the product; the object is the product; the requirements (length, width, height, radius, etc.) are the attributes of the class in the class.

When we want to produce a product, first we have to clarify the requirements of the product according to the demander. For example, if we want to make a batch of rectangular products (product a, product b, product c), our CAD mold (type ), the requirements are length, width, and height indicators (class attributes). What is further needed are the specific parameters (class attribute parameters) of the various indicators of products a, b, c, length, width and height.

Suppose you are a technician in a factory, how do you operate CAD molds?

The first method:

class长方形产品a模具:
{
private $长=1.1m;
private $宽=1.2m;
private $高=1.5m; 
}
$产品a=new class 长方形产品a();
class 长方形产品b模具:
{
private $长=1.4m;
private $宽=1.7m;
private $高=1.8m; 
}
$产品b=new class 长方形产品b();
class 长方形产品c模具:
{
private $长=1.0m;
private $宽=1.1m;
private $高=0.9m; 
}
$产品b=new class 长方形产品c();

The second method: use the construction method is

class 长方形产品模具类:
{
private $长;
private $宽;
private $高;
public function __construct(参数1, 参数2, 参数3){
$this->长 =参数1;
$this->宽 =参数2;
$this->高= 参数3;
}
实例化各产品对象的时候,我们只需要
$产品a=new class长方形产品cad模具类(1.1m,1.2m,1.5m);
$产品b=new class长方形产品cad模具类(1.4m,1.7m1.8m);
$产品c=new class长方形产品cad模具类(1.1m,1.0m1.9m);
....

If it is not produced For products with regular shapes, technicians will need to set a lot of CAD mold (class) indicators (class attributes). If the batch of products has many sizes and types (many objects), a lot of indicator parameters (class attribute parameters) need to be set. , obviously the latter second method will be much more efficient.

In other words, the constructor method __construct() has a great advantage when instantiating a class object. It can create a class once and reuse it many times. Here, we also understand that the advantage of the constructor is that it can accept parameters. When instantiating an object, different attribute initialization values ​​are set for different objects.

The above is the detailed content of The role of php constructor. 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