慢慢长寻夜,明月高空挂

__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. asp.net 实现“九连环”小游戏

    asp.net 实现"九连环"小游戏 wildcatsky(原作) public Class ChinaRing1  Inherits System.Web.UI.Page #Re ...

  2. iOS开发——UI_swift篇&TableView自定义聊天界面

    TableView自定义聊天界面   1,下面是一个放微信聊天界面的消息展示列表,实现的功能有: (1)消息可以是文本消息也可以是图片消息 (2)消息背景为气泡状图片,同时消息气泡可根据内容自适应大小 ...

  3. MYSQL 5.7 新增150多个新功能

    http://www.thecompletelistoffeatures.com/ There are over 150 new features in MySQL 5.7. The MySQL ma ...

  4. OSGi 学习(二)

    上一篇说了很多虚的东西,现在说点别的. OSGi系统的独立环境下的项目结构以及启动脚本. 先说项目结构,基于equinox的OSGi容器的项目结构如下所示: bin中定义启动脚本,停止脚本之类的. c ...

  5. css 设置全屏背景图片

    <div id="div1"><img src="img.jpg" /></div> div#div1{ position: ...

  6. MySQL大批量插入数据

    MySQL大批量插入数据 1. 对于Myisam类型的表,可以通过以下方式快速的导入大量的数据. ALTER  TABLE  tblname  DISABLE  KEYS; loading  the  ...

  7. oracle数据库元数据SQL查询

    oracle数据库经典SQL查询 .查看表空间的名称及大小 select t.tablespace_name, round(sum(bytes/(1024*1024)),0) ts_size from ...

  8. UTF8 与 UTF16 编码

    Unicode 的发展,英文好的直接去 unicode.org 上去看吧,不好的可以移步到这里 看dengyunze的总结:<关于UTF8,UTF16,UTF32,UTF16-LE,UTF16- ...

  9. ORM之PetaPoco入门(二)--Petapoco基本用法

    1. Petapoco基本用法 1.1. 创建示例工程 首先创建一个工程文件,为了便于展示数据这里创建一个类型为:WindowsApplication的工程文件.命名为:PetapocoTest. 程 ...

  10. Linux逻辑卷创建

    1.创建PV物理卷 [root@localhost ~]# pvcreate /dev/sda4 /dev/sda5 /dev/sda6 /dev/sda7    Physical volume “/ ...