Home > Backend Development > PHP Tutorial > Detailed explanation of magic functions in PHP object-oriented development_PHP tutorial

Detailed explanation of magic functions in PHP object-oriented development_PHP tutorial

WBOY
Release: 2016-07-13 10:50:20
Original
839 people have browsed it

Magic functions in PHP include tostring, call, clone, and autoload. I hope this tutorial will be helpful to all my friends.

Summary of object-oriented development

1. Description and configuration of objects. (can be understood as the output text description of the object)
2. A processing of object method. (It can be understood as customizing a more humane error message when an exception occurs)
3. Application of cloned objects. (It can be understood as cloning another object that is the same as the original object. Note that it is another one.)
4. Method to automatically load objects. (Can be understood as a quote)

1. Object description and configuration

Method name: __tostring()
Format:

The code is as follows Copy code
class My {
 代码如下 复制代码
class My {
function __tostring() {
return "这里写这个类的文字描述"; //要用return,echo会出错。
}
}
$p = new My();
echo $p;
function __tostring() {

return "Write a text description of this class here"; //If you use return, echo will make an error.

}
 代码如下 复制代码
class My {
function __toString() {
return "这个类的作用是,保卫地球。";
}
}
$p = new My();
echo $p;
?>
}

$p = new My(); echo $p;

Example:
The code is as follows Copy code
class My {
function __toString() {
return "The purpose of this class is to protect the earth.";

}
代码如下 复制代码
class My {
function __call($n,$v) {
echo "错误的方法名:".$n;
echo "错误的参数:".$v;
}
}
}

$p = new My();

echo $p;
代码如下 复制代码

class My {
function __toString() {
return "这个类的作用是,保卫地球。";
}

function __call ($n,$v) {
echo "错误的方法".$n."
";
    echo "错误的值".print_r($v).""; //值是以数组方式传递的,所以要用print_r。
 }
}
$p = new My();
$p->demo("第一",6);
?>

?>

2. Exception handling of object methods

Exception handling when calling some non-existent object methods allows the program to run normally.
Method name: __call($funname,$arr_value)
Format:

The code is as follows Copy code
class My { function __call($n,$v) {
 代码如下 复制代码
class My {
function __clone() {
echo "克隆时自动调用的函数";
}
}
$a = new My();
$b = clone $a;
echo "Wrong method name:".$n; echo "Wrong parameter:".$v; } }
Example:
The code is as follows Copy code
".$n."
"; echo "Wrong value".print_r($v).""; //The value is passed in an array, so print_r is used. } } $p = new My(); $p->demo("First",6); ?>
3. Cloning of objects Clone can generate two identical objects in memory or upgrade the original object. (It is not a simple assignment, but two blocks of memory are opened in the memory. The cloned and the cloned are two different objects with the same attributes and methods) Method name: __clone() Keyword: clone Format:
The code is as follows Copy code
class My { function __clone() { echo "Function automatically called when cloning"; } } $a = new My(); $b = clone $a;


Example:

$p = new My(); $b = clone $p;
The code is as follows
 代码如下 复制代码

class My {
 public $name = "小猪";
 function __toString() {
  return "这个类的作用是,保卫地球。";
 }

 function __call ($n,$v) {
  echo "错误的方法".$n."
";
    echo "错误的值".print_r($v).""; //值是以数组方式传递的,所以要用print_r。
 }
}
$p = new My();
$b = clone $p;

echo $b->name="小猪头"."
"; //这是克隆并修改后的对象,如果使用 $b = $p,那修改$b->name="小猪头";后,再输出$p,也会被修改。
echo $p->name; //这是原对象,它已经被$b克隆了,其他都一样,但是他们是独立的。
?>

Copy code


class My { public $name = "Little Pig";

function __toString() {
return "The purpose of this class is to protect the earth.";
}

function __call ($n,$v) {
 代码如下 复制代码
function __autoload($class_n) {
include($class_n.".php");
}
$p = new MyPc(); //自动调用执行如下
$d = new demo(); //include("mypc.php")
echo "Wrong method".$n."
";

echo "Wrong value".print_r($v).""; //The value is passed in an array, so print_r is used.

}
 代码如下 复制代码

function __aotoload ($class_name) {
include($class_name.".php"); //写在类外部。
}

$p = new class_name(); //这里实例化的类名就被上面的函数变量 $class_name 替代了,非常方便。
?>

}
echo $b->name="Little Pig Head"."
"; //This is the cloned and modified object. If $b = $p is used, then modify $b-> ;name="Little Pig Head";, and then output $p, it will also be modified.

echo $p->name; //This is the original object, which has been cloned by $b. Everything else is the same, but they are independent.

?> 4. Method of automatically loading objects Quickly get the object name and automatically load it into the current page Method name: __autoload()
Format:
The code is as follows Copy code
function __autoload($class_n) { include($class_n.".php"); } $p = new MyPc(); //The automatic call is executed as follows $d = new demo(); //include("mypc.php") Example:
The code is as follows Copy code
function __aotoload ($class_name) {<🎜> include($class_name.".php"); //Written outside the class. <🎜> }<🎜> <🎜>$p = new class_name(); //The class name instantiated here is replaced by the function variable $class_name above, which is very convenient. <🎜> ?> http://www.bkjia.com/PHPjc/632640.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632640.htmlTechArticleThe magic functions in php include tostring, call, clone and autoload. I hope this tutorial will be useful to you. Friends can help. Summary of object-oriented development 1. Description and configuration of objects...
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