慢慢长寻夜,明月高空挂

__isset()    - 在对类中属性或者非类中属性使用isset()方法的时候如果没有或者非公有属性,则自动执行__isset()的方法

__unset()  - 在对类中属性或者非类中属性使用unset()方法的时候如果没有或者非公有属性,则自动执行__unset()的方法

<?php
/**
* 针对类中的魔术方法 __isset() 和 __unset() 的例子
*/ class Example {
public $public;
protected $protected;
private $private; public function __construct(){
$this->public = 'pub';
$this->protected = 'pro';
$this->private = 'pri';
} public function __isset($var){
echo '这里通过__isset()方法查看属性名为 '.$var."\n";
} public function __unset($var){
echo '这里通过__unset()方法要销毁属性名为 '.$var."\n";
}
} $exa = new Example; echo '<pre>';
var_dump(isset($exa->public));
echo "\n";
var_dump(isset($exa->protected));
echo "\n";
var_dump(isset($exa->private));
echo "\n";
var_dump(isset($exa->noVar));
echo "\n";
echo '<hr/>'; unset($exa->public);
var_dump($exa); echo "\n";
unset($exa->protected);
echo "\n";
unset($exa->private);
echo "\n";
unset($exa->noVar);
echo "\n";

结果如下:

bool(true)

这里通过__isset()方法查看属性名为 protected
bool(false) 这里通过__isset()方法查看属性名为 private
bool(false) 这里通过__isset()方法查看属性名为 noVar
bool(false)
------------------------------------------------------------------------------
object(Example)# () {
["protected:protected"]=>
string() "pro"
["private:private"]=>
string() "pri"
} 这里通过__unset()方法要销毁属性名为 protected 这里通过__unset()方法要销毁属性名为 private 这里通过__unset()方法要销毁属性名为 noVar

PHP 魔术方法 __isset __unset (三)的更多相关文章

  1. PHP 魔术方法之__set__get__unset,__isset,__call

    <?php /*** 魔术方法: 是指某些情况下,会自动调用的方法,称为魔术方法 PHP面向对象中,提供了这几个魔术方法, 他们的特点 都是以双下划线__开头的 __construct(), _ ...

  2. HP叫魔术方法的函数

    PHP5.0后,php面向对象提成更多方法,使得php更加的强大!! 一些在PHP叫魔术方法的函数,在这里介绍一下:其实在一般的应用中,我们都需要用到他们!! 1.__construct() 当实例化 ...

  3. PHP中的魔术方法:__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep, __wakeup, __toString, __set_state, __clone and __autoload

    1.__get.__set 这两个方法是为在类和他们的父类中没有声明的属性而设计的: __get( $property ) 当调用一个未定义的属性时访问此方法: __set( $property, $ ...

  4. __get().__set.__isset,__unset魔术方法

    一般来说,总是把类的属性定义为 private .这更符合现实的逻辑. 但是对属性的读取和赋值操作非常频繁的,因此在PHP中,预定义了两魔术方法 "__get()"用来获取私有成员属性值的,只有一个参 ...

  5. PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep, __wakeup, __toStr

    PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep ...

  6. PHP:__get()、__set()、__isset()、__unset()、__call()、__callStatic()六个魔术方法

    哎呀呀,今天小仓鼠学到了魔术方法,简称魔法,哈哈哈哈,神经病啊~ 平时在面试的时候,也会遇到问魔术方法有哪些的问题哦!今天我们来了解一下下~ 1.__get() 形式: __get($objName) ...

  7. PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep

    PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep ...

  8. PHP中的魔术方法总结:__construct,__destruct ,__call,__callStatic,__get,__set,__isset, __unset ,__sleep,__wakeup,__toString,__set_state,__clone,__autoload

    1.__get.__set这两个方法是为在类和他们的父类中没有声明的属性而设计的__get( $property ) 当调用一个未定义的属性时访问此方法__set( $property, $value ...

  9. __unset()魔术方法 删除类内私有属性

    __unset()魔术方法 删除私有属性 unset()对共有属性进行删除 可通过__unset()魔术方法对私有属性进行操作 当在类外部执行unset()函数时,自动执行类内__unset()魔术方 ...

随机推荐

  1. 理解WebKit和Chromium: 调试Android系统上的Chromium

    转载请注明原文地址:http://blog.csdn.net/milado_nju 1. Android上的调试技术 在Android系统上,开发人员能够使用两种不同的语言来开发应用程序,一种是Jav ...

  2. Java中Queue类实现

    原先在java编程中,Queue的实现都是用LinkedList Queue queue = new LinkedList(); 但正如jdk中所说的那样: 注意,此实现不是同步的.如果多个线程同时访 ...

  3. jQuery訪问属性,绝对定位

    一. jQuery訪问属性 <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...

  4. ASCII与UNICODE的区别

    1.ASCII的特点 ASCII 是用来表示英文字符的一种编码规范.每个ASCII字符占用1 个字节,因此,ASCII 编码可以表示的最大字符数是255(00H—FFH).这对于英文而言,是没有问题的 ...

  5. 《C和指针》之ANSI C标准输入输出函数

    一.I/O流操作一般流程: (1)为每一个要打开的文件定义一个FILE *类型的指针变量,这个指针变量将指向I/O流使用的FILE结构体. (2)使用fopen函数打开I/O流.要打开一个I/O流,必 ...

  6. Spreadsheet Tracking

     Spreadsheet Tracking  Data in spreadsheets are stored in cells, which are organized in rows (r) and ...

  7. Calendar Game

    http://poj.org/problem?id=1082 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4820   A ...

  8. Honda HDS IMMO PCM Code calculator Free Download

    HDS IMMO PCM Code calculator software for Honda vehicle models is free download available in Eobd2.f ...

  9. this的分析

    this是js中的一个关键字,当函数运行时,会自动生成的一个对象,只能在函数内部使用.随着函使用场合不同,this值会变化,但是始终指向调用函数的那个对象. 1.纯粹的函数调用 function bo ...

  10. linux编辑器 vi的使用

    vi 编辑器的三个模式: 命令模式 开始就是命令模式 insert模式 i, I, a, A, o,O ,s, S 命令行模式 : i  在当前位置进入Insert模式 I  在行的头部进入Inser ...