PHP面向对象开发之魔术函数详解
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 __call ($n,$v) { |
3.对象的克隆
通过克隆方式可以在内存中生成两个一样的对象或升级原对象。(不是简单的赋值,而是在内存中开两块内存,克隆出的和被克隆的是2个属性方法一样的不同的两个对象)
方法名:__clone()
关键字:clone
格式:
代码如下 | 复制代码 |
class My { function __clone() { echo "克隆时自动调用的函数"; } } $a = new My(); $b = clone $a; |
实例:
代码如下 | 复制代码 |
class My { function __call ($n,$v) { echo $b->name="小猪头"." |
4.自动载入对象的方法
快速取得对象名称并自动载入进当前页面
方法名:__autoload()
格式:
代码如下 | 复制代码 |
function __autoload($class_n) { include($class_n.".php"); } $p = new MyPc(); //自动调用执行如下 $d = new demo(); //include("mypc.php") |
实例:
代码如下 | 复制代码 |
function __aotoload ($class_name) { $p = new class_name(); //这里实例化的类名就被上面的函数变量 $class_name 替代了,非常方便。 |

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

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
Powerful PHP integrated development environment

SublimeText3 Mac version
God-level code editing software (SublimeText3)

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
The latest (2018.2.1) professional PHP integrated development tool