Home > Backend Development > PHP Tutorial > Several magic methods commonly used in PHP

Several magic methods commonly used in PHP

一个新手
Release: 2023-03-15 19:12:02
Original
2865 people have browsed it

Commonly used magic methods are: __Tostring () __Call() __autoLoad() __ clone() __GET() __SET() __isset() __unset()

1.__Tostring() Used to be called when defining an output object reference Commonly used to print some object informationMust have a return value##

eg:有一个persion类
Persion per =new persion()
Echo per;    //直接调用会出错
我们可以在类的定义中添加__tostring()方法
Function  __Tostring()
{
$str=this->$name.this->age;
Return $str;
}
Copy after login


2.__clone()Copy of object

Reference assignment

##$per1=$per2;

And this has only one address in the memory

And$per1=clone $per2 There are two memory addresses at this time

3.__call()MethodWhen a function that does not exist in a class instance is called Automatic execution

If you try to call a function that does not exist in the class, a syntax error will occur. In order to provide a friendly prompt

We can declare Call()method in the class;

Function __call($funName,$argu)
{
Echo "名为".$funName."参数为".printf($argh)."的函数不存在",
}
Copy after login


##4.__autoLoadAutomatic loading The class file used. This function is to add

to the referenced page. We have all used this situation. In the page, we need To call other

php files, we need to use the include method

但是如果有几十个页面需要引用,未免太过繁琐,我们可以在该页面中使用autoload方法

Function __autoload($className)
{
Include $className.".php";
}
Copy after login


这样凡是引用到其他类的地方,都会自动引用该类文件 前提类文件的名称必须是 类名.php

5.__GET() 访问类中私有属性

如果类中的属性设置为私有属性,在类的实例中是无法访问的,但怎样才能访问呢?

我们就可以使用__GET()

Eg

类中有

Class person
{
Private $name;
Private $age;
}
Copy after login


实例化

person per=new person()
Per->$name; //这样是取不到值的
Copy after login


但是如果我们在类中增加__GET方法

Function __GET($proName)
{
Return this->$proName;
}
Copy after login


我们再次调用Per->$name就可以访问了

这样做有人会提出疑问了,这样可以直接访问私有变量,和声明成公有的有什么区别呢?

如果声明成公有的,我们可以任意读取,如果是私有,如果我们增加了get方法,那么每次调用私有属性都会调用GET方法的内容,这样我们就可以在get方法中增加一些逻辑处理。

6.__SET()设置类中的私有属性

原理同上,我们可以再类中添加__SET()函数,每当通过调用类实例给私有属性赋值,都会执行__SET函数,函数原型:

Function __SET($proName,$value)
{
This->$proName=$value;
}
Copy after login


既然是方法赋值,我们就可以做一些逻辑处理

7.__isset()判断类中私有属性或方法是否存在时自动调用

首先我们先介绍一下isset方法,该方法用于判定属性和方法是否存在,但是我们无法通过类类实例判断类中的某个私有属性是否存在

如果我们使用isset(per->$name);//返回值是false,但是$name属性的确存在,怎么解决呢?
解决方法:

1.$name定义为私有属性

2.

在类定义中添加

Function __isset($proName)
{
Return  isset(this->$proName);//再类内部是可以访问私有属性的
}
Copy after login


这样的话我们再次调用isset($name);返回值就为true了;

8.__unset()清除类中私有变量时自动调用

与之结合的是unset() unset方法可以删除属性,当我们需要删除类中属性的时候,如果是公有属性我们可以直接

删除,但是如果是私有我们只通过该方法就无法实现了

怎样实现呢我们可以使用__unset()方法实现该功能我们需要在类中添加

Function __unset($proName)
{
Unset(this->$proName);
}
Copy after login


现在我们再调用unset($name);就可以删除person类中的私有属性$name了

The above is the detailed content of Several magic methods commonly used 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