PHP魔术方法

猫斯基 27 2014-01-20

魔术方法

__construct(),构造函数,在创建对象时自动调用。

__destruct(),析构函数,在释放对象前自动调用。

__get(),在外部直接访问私有成员时自动调用。

__set(),在外部直接设置私有成员的值时自动调用。

__isset(),在外部使用isset()判断一个私有属性是否存在时,自动调用。

__unset(),在外部使用unset()删除一个私有属性时,自动调用。

__toString(),在外部直接使用echo printf等输出一个对象引用时,自动调用。将对象的基本信息放在这个方法内部,形成字符串返回。此方法中,不能有参数,且必须返回一个字符串。

__clone(),使用clone关键字复制一个对象时,自动调用。和构造方法一样,不过是对克隆的对象进行初始化;此方法中的$this代表的是克隆体。

__call(),在外部使用一个对象中不存在的方法时,自动调用。有两个参数,第一个参数是调用不存在的方法名,第二参数是调用不存在的方法参数。

作用:1、可以写提示。2、简化方法功能相似但方法名称不同的方法的调用。

__set_state(),使用var_export()方法时,导出一个类的信息时自动调用。此方法必须是静态的,且有一个数组参数。

PHP魔术方法不写就不存在,没有默认功能。

eval()函数 – 检查并执行PHP代码。

$str = "echo 'abc';";
eval( $str ); // abc

var_dump()函数,返回传递给该函数的变量的结构信息。

var_export()函数,返回传递给该函数的变量的结构信息,且必须返回的是合法的php代码。也就是说,var_export()返回的代码,可以直接当做php代码赋值给一个变量。

$arr = array( 'one'=>1, "two"=>'2' );
var_dump( $arr );
// array(2) { ["one"]=> int(1) ["two"]=> string(1) "2" }
var_export( $arr );
// array ( 'one' => 1, 'two' => '2', )
eval( '$a = ' . var_export( $arr, true ) . ';' );
print_r( $a );
// Array ( [one] => 1 [two] => 2 )

__get() __set()

class wifeClass {
	// 成员属性
	private $name;
	private $age;
	private $sex;
	function __construct( $name, $age, $sex ) {
		echo "构造方法
"; $this->name = $name; $this->age = $age; $this->setSex($sex); } function __get( $proname ) { return $this->$proname.' ---__get
'; } function __set( $key, $value ) { echo "{$key} = {$value}".'---__set
'; if ( $key == 'age' ) { if ( $value < 0 || $value > 100 ) return; } $this->$key = $value; } public function setSex( $sex ) { if ( !($sex=='男' or $sex == '女') ) return; $this->sex = $sex; } public function say() { echo "{$this->name} 说话了:我是{$this->sex}性,{$this->age}岁了。
"; } function __destruct() { echo "{$this->name} 再见!
"; } } $wife = new wifeClass('Nox', '31', '女'); $wife->say(); echo $wife->name; $wife->age = "18"; $wife->say(); // 直接调用 echo $wife->__get('name'); /* 打印结果: 构造方法 Nox 说话了:我是女性,31岁了。 Nox ---__get age = 18---__set Nox 说话了:我是女性,18岁了。 Nox ---__get Nox 再见! */

__isset() __unset()

/**
 * @url https://www.maosiji.com
ds
 */
class wifeClass {
	// 成员属性
	private $name;
	private $age;
	private $sex;
	function __construct( $name, $age, $sex ) {
		echo "构造方法
"; $this->name = $name; $this->age = $age; $this->setSex($sex); } function __isset( $proname ) { return $this->$proname.' --- __isset
'; } function __unset( $proname ) { echo "{$proname}".'--- __set
'; if ( $proname != 'age' ) { unset( $this->$proname); } } public function setSex( $sex ) { if ( !($sex=='男' or $sex == '女') ) return; $this->sex = $sex; } public function say() { echo "{$this->name} 说话了:我是{$this->sex}性,{$this->age}岁了。
"; } function __destruct() { echo "{$this->name} 再见!
"; } } $wife = new wifeClass('Nox', '31', '女'); $wife->say(); unset( $wife->name ); $wife->say(); // 直接调用 echo isset($wife->name); /* 打印结果: 构造方法 Nox 说话了:我是女性,31岁了。 name--- __set 说话了:我是女性,31岁了。 1 再见! */

__toString()

/**
 * @url https://www.maosiji.com
ds
 */
class Person {
	function __toString() {
		return 'aad';
	}
}
$p = new Person;
echo $p;
/*
打印结果:
aad
*/

__clone()

/**
 * @url https://www.maosiji.com
ds
 */
class Person {
	public $name;
	public $age;
	public $sex;
	function __construct( $name, $age, $sex ) {
		$this->name = $name;
		$this->age = $age;
		$this->sex = $sex;
	}
	function __toString() {
		return 'aad';
	}
	function say() {
		echo "i am {$this->name}, age {$this->age}, sex {$this->sex}.
"; } function __clone() { $this->name = "克隆体"; $this->age = 0; } function __destruct() { echo $this->name.'释放了
'; } } $p = new Person( 'Ad', 15, 'man' ); $p->say(); $p2 = clone $p; $p2->say(); /* 打印结果: i am Ad, age 15, sex man. i am 克隆体, age 0, sex man. 克隆体释放了 Ad释放了 */

__call()

/**
 * @url https://www.maosiji.com
 */
class Person {
	public $name;
	public $age;
	public $sex;
	function __construct( $name, $age, $sex ) {
		$this->name = $name;
		$this->age = $age;
		$this->sex = $sex;
	}
	function __call( $method, $args ) {
		echo "调用的方法{$method},参数为";
		print_r( $args );
		echo "不存在!
"; } function say() { echo "i am {$this->name}, age {$this->age}, sex {$this->sex}.
"; } function __destruct() { echo $this->name.'释放了
'; } } $p = new Person( 'Ad', 15, 'man' ); $p->eat('ddd', 'www'); /* 打印结果: 调用的方法eat,参数为Array ( [0] => ddd [1] => www ) 不存在! Ad释放了 */
/**
 * @url https://www.maosiji.com
 */
class Person {
	public $name;
	public $age;
	public $sex;
	public $darr = array("a", "b", "c");
	function __construct( $name, $age, $sex ) {
		$this->name = $name;
		$this->age = $age;
		$this->sex = $sex;
	}
	function __call( $method, $args ) {
		if ( in_array( $method, $this->darr ) ) {
			echo $args[0].'
'; } else { echo "调用的方法 {$method} 不存在!
"; } } function __destruct() { echo $this->name.'释放了
'; } } $p = new Person( 'Ad', 15, 'man' ); $p->a('aaa'); $p->b('bbb'); $p->c('ccc'); /* 打印结果: aaa bbb ccc Ad释放了 */

__set_state()

class Person {
	public $name;
	public $age;
	public $sex;
	public function __construct( $name, $age, $sex ) {
		$this->name = $name;
		$this->age = $age;
		$this->sex = $sex;
	}
	function say() {
		echo "我叫{$this->name},今年{$this->age}岁
"; } public static function __set_state( $arr ) { $w = new Person( 'sun', 50, '女' ); $w->name = $arr['name']; $w->age = $arr['age']; return $w; } } $o = new Person( 'xmoon', 18, '男' ); $o->name = '王五'; $o->age = 28; eval( '$a = ' . var_export($o, true) . ';' ); var_dump( $a ); /* 打印结果: object(Person)#2 (3) { ["name"]=> string(6) "王五" ["age"]=> int(28) ["sex"]=> string(3) "女" } */

本文由 猫斯基 原创发布。

著作权均归用户本人所有。独家文章转载,请联系本站管理员。获得授权后,须注明本文地址! 本文地址:https://www.maosiji.com/2553.html