php面向对象类中常用的魔术方法
Posted 小面团
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php面向对象类中常用的魔术方法相关的知识,希望对你有一定的参考价值。
1、__construct():构造方法,当类被实例化new $class时被自动调用的方法,在类的继承中可以继承与覆盖该方法,例:
//__construct()
class construct{
public function __construct(){
$this->var = "this is var";
}
}
class con2 extends construct{
public function __construct(){
$this->var = "this is var2";
}
}
$con2 = new con2;
echo $con2->var;
3、__destruct():当对象销毁或者不再使用时(页面运行完),自动调用这个方法,例:
class construct{
public function __construct(){
$this->var = "this is var";
}
public function __destruct(){
echo \'close\';
}
}
$con = new construct;
//或者最常用的关闭资源
class destruct{
public function __construct(){
$this->logfile_handle = fopen(\'/tmp/log.txt\',w);
}
public function __destruct(){
fclose($this->logfile_handle);
}
}
4、__toString():该方法用于一个类被当成字符串时应怎么样回应,也就是echo $class时返回的内容是什么,如果没有__toString()魔术方法时,将会返回一个E_RECOVERABLE_ERROR
级别的致命错误。
例:
<?php
class TestClass{
public $foo;
public function __construct($foo){
$this->foo = $foo;
}
public function __toString(){
return $this->foo;
}
}
$class = new TestClass(\'Hello\');
echo $class;//Hello
?>
5、__invoke():该魔术方法用于类被当作函数使用时,自动调用该方法,但是...__invoke()魔术方法只在PHP5.3.0以上才有效;例
//__invoke() PHP5.3.0以上版本有效
class CallableClass{
function __invoke($x){
var_dump($x);
}
}
$obj = new CallableClass;
$obj(5);
var_dump(is_callable($obj));//true
6、__get()与__set():从一个难以访问的属性读取数据是自动调用__get()魔术方法,从一个难以访问的熟悉赋值的时候自动调用__set()魔术方法,难以访问包括(私有属性,未定义属性、没有初始化的属性)例:
//__get() __set()
class demoClass{
private $a;
private function __get($name){
return \'this var not exist\';
}
private function __set($name,$value){
echo \'this var value not exist\';
}
}
$demo = new demoClass();
var_dump($demo->a);//调用__get()方法
$demo->a = 1;//调用__set()方法
7、__call():该方法在范围类中不存在的方法时自动调用的方法,参数有两个;例:
//__call()
class funClass{
public function __call($name,$arg){
echo $name.\'function not exist\';
}
}
$fun = new funClass();
$fun->aa();//function not exist
8、__sleep()和__wakeup:这两个魔术方法是在对象序列化serialize()和解序列化unserialize()自动调用的方法:例
//__sleep __wakeup 简单实例
class sleepClass{
public $myContent;
public function __construct($string){
$this->myContent = $string;
}
public function __sleep(){
$this->myContent = "this is mimi";
return array(\'myContent\');
}
public function __wakeup(){
echo \'aaa\';
}
}
$sleep = new sleepClass(\'i love bb\');
$ser = serialize($sleep);//this is mimi
unserialize($ser);//aaa
//或者 常用实例
class Connection {
protected $link;
private $server, $username, $password, $db;
public function __construct($server, $username, $password, $db)
{
$this->server = $server;
$this->username = $username;
$this->password = $password;
$this->db = $db;
$this->connect();
}
private function connect()
{
$this->link = mysql_connect($this->server, $this->username, $this->password);
mysql_select_db($this->db, $this->link);
}
public function __sleep()
{
return array(\'server\', \'username\', \'password\', \'db\');
}
public function __wakeup()
{
$this->connect();
}
}
$con = new Connection(\'127.0.0.1\',\'root\',\'\',\'mythinkcom\');
echo serialize($con);
9、__clone():$obj2 = clone $obj1与$obj2 = $obj1的区别是前者有两块内存地址,而后者只有一块内存地址。
class cloneClass{
public $var = \'aa\';
}
$clone = new cloneClass;
$clone2 = clone $clone;
echo $clone->var;
echo $clone2->var;