search
Homephp教程php手册PHP面向对象开发之魔术函数详解

PHP面向对象开发之魔术函数详解

Jun 13, 2016 am 09:52 AM
phptostringfunctionobjectopenhaveDetailed explanationFormagic

php中魔术函数包括有tostring、call、clone、autoload几个常用的函数,希望此教程对各位朋友会有所帮助。

面向对象开发总结

1.对象的描述和配置。(可以理解为对对象的输出文字描述)
2.对象方法的一场处理。(可以理解为出现异常时候,自定义更人性化的错误信息)
3.克隆对象的应用。(可以理解为在原始对象上,克隆出一样的另一个对象,注意是另一个。)
4.自动载入对象的方法。(可以理解为引用)

1.对象描述和配置

方法名:__tostring()
格式:

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

实例:

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

2.对象方法的异常处理

调用一些不存在的对象方法的异常处理,是程序正常运行。
方法名:__call($funname,$arr_value)
格式:

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

实例:

 代码如下 复制代码

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

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

3.对象的克隆

通过克隆方式可以在内存中生成两个一样的对象或升级原对象。(不是简单的赋值,而是在内存中开两块内存,克隆出的和被克隆的是2个属性方法一样的不同的两个对象)
方法名:__clone()
关键字:clone
格式:

 代码如下 复制代码
class My {
function __clone() {
echo "克隆时自动调用的函数";
}
}
$a = new My();
$b = clone $a;


实例:

 代码如下 复制代码

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克隆了,其他都一样,但是他们是独立的。
?>


4.自动载入对象的方法


快速取得对象名称并自动载入进当前页面
方法名:__autoload()
格式:

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

实例:

 代码如下 复制代码

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

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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool