1、__call
__call( $method, $arg_array ) 当调用一个未定义的方法是调用此访求,php5 的对象新增了一个专用方法 __call(),这个方法用来监视一个对象中的其它.
方法:如果你试着调用一个对象中不存在的方法,__call 方法将会被自动调用.
__call实例方法代码如下:
<?php class foo { function __call($name,$arguments) { print("did you call me? i'm $name!"); } } $x = new foo(); $x->dostuff(); $x->fancy_stuff();
这个特殊的方法可以被用来实现"过载(overloading)"的动作,这样你就可以检查你的参数并且通过调用一个私有的方法来传递参数.
2、__autoload __autoload 函数,它会在试图使用尚未被定义的类时自动调用,看下面的实例,写好了一个msyql类,mysql.php: class mysql{ funciton __construct(){ ............ } }
现在我在index.php页面要用到mysql 类,我就这样,代码如下:
function __authload($class){ include_once("path".$class.".php"); } $mysql=new mysql(); include_once("path/".$class.".php");
path/ 是类文件所在路径
$class 就是调用时的类名啦
后面的.php 当然是扩展名啦,一个类文件可能感觉不到有多好用,如果类文件很多的时候,每个类都要include一下,那太麻烦了,只要每个页面之前写一个 __autoload() 即可,通过调用此函数,脚本引擎在 php 出错失败前有了最后一个机会加载所需的类.
3、__construct、__destruct
构造函数与析构函数[__construct __destruct()],他在类class中的作用是初始化与销毁变量下面我们来看看实例:
class db { function __construct() { $this->mconnid=mysql_connect ($this->dbhost,$this->dbuser,$this->dbpwd);//建立连接 mysql_select_db($this->dbname, $this->mconnid); //选择数据库 mysql_query("set names 'gbk'");//设置数据库教程编码为gbk } //__destruct:析构函数,断开连接 function __destruct() { mysql_close($this->mconnid); //此处还有问题...... } }
这时我们在用时就不需要考虑数据连接与关闭了,只要$aa = new db();就ok了.
4、__clone
__clone魔术方法,我们知道对象是可以直接赋值的,比如
$p2 = $p1;//这里是一个对象有两个引用,那么我执行:$p1->say();$p2->say();是都可以执行的,而且效果一样.
我们还有一种方法:$p3 = clone $p1;//注意clone是克隆关键字,这里与上面的不同是$p3是一个新的对象,同时我们在类里加入一个方法:
function __clone() { $this->name = "我是副本"; //注意:这里的$this是克隆产生的对象本身,不 是当前类 }
//然后我们执行:$p3->say();
//打印出:name:我是副本 age:20
到这里我们明白,__clone()方法是在克隆对象的时候执行的方法,它的作用是对新克隆出来的副本进行属性初始化等操作.
5、__tostring
__tostring方法在将一个对象转化成字符串时自动调用,如果我有一个类:
class person { private $name = ""; private $age = 0; function __construct($name = "", $age = "") { $this->name = $name; $this->age = $age; } function say() { echo "name:".$this->name."<br/>"."age:".$this->age."<br/>"; } }
现在我去实例化这个类,然后去打印这个实例:
$p1 = new person("liuzy",20);echo $p1; //直接打印会出错
显然这样直接打印对象是会出现错误的,因为对象是引用句柄,不能直接打印,这时,我们可以用到__tostring()方法,我们在person类里加一个__tostring()方法:
function __tostring() { return "i am person,my name is ".$this->name."<br/>"; }
然后再刷新页面,发现什么了?现在我们明白,__tostring()是在直接打印对象时执行的方法,我们可以用该方法打印类的一些相关信息,注意:是两个下划线,方法必须有返回值.
6、__sleep、__wakeup
__sleep 串行化的时候用,__wakeup 反串行化的时候调用,在php进行序列化时,serialize() 检查类中是否有 __sleep(),如果有,则该函数将在任何序列化之前运行,该函数必须返回一个需要进行序列化保存的成员属性数组,并且只序列化该函数返回的这些成员属性.
该函数有两个作用:第一,在序列化之前,关闭对象可能具有的任何数据库连接等,第二,指定对象中需要被序列化的成员属性,如果某个属性比较大而不需要储存下来,可以不把它写进__sleep要返回的数组中,这样该属性就不会被序列化,相反地,unserialize() 从字节流中创建了一个对象之后,马上检查是否具有__wakeup 的函数的存在,如果存在,__wakeup 立刻被调用,使用 __wakeup 的目的是重建在序列化中可能丢失的任何数据库连接以及处理其它重新初始化的任务.
<?php class user { public $name; public $id; function __construct() { $this->id = uniqid(); //give user a unique id 赋予一个不同的id } function __sleep() { return(array("name")); //do not serialize this->id 不串行化id } function __wakeup() { $this->id = uniqid(); //give user a unique id } } $u = new user; $u->name = "haha"; $s = serialize($u); //serialize it 串行化 注意不串行化id属性,id的值被抛弃 $u2 = unserialize($s); //unserialize it 反串行化 id被重新赋值 //$u and $u2 have different ids $u和$u2有不同的id var_dump($u); var_dump($u2);
---------- php debug ----------
object(user)#1 (2) { ["name"]=> string(4) "haha" ["id"]=> string(13) "47fa045529f69" } object(user)#2 (2) { ["name"]=> string(4) "haha" ["id"]=> string(13) "47fa04552a49a" }
本文地址:
转载随意,但请附上文章地址:-)

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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),

SublimeText3 Linux new version
SublimeText3 Linux latest version

Notepad++7.3.1
Easy-to-use and free code editor

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools
