慢慢长寻夜,明月高空挂

__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. JAVA线程池简介

    一 简介 线程的使用在java中占有极其重要的地位,在jdk1.4极其之前的jdk版本中,关于线程池的使用是极其简陋的.在jdk1.5之后这一情况有了很大的改观.Jdk1.5之后加入了java.uti ...

  2. openstack 创建虚拟机

    http://blog.csdn.net/tantexian/article/details/44595885#comments

  3. Anaconda packages list

    # packages in environment at D:\Applications\Anaconda3:#alabaster 0.7.6 py35_0 anaconda 2.4.0 np110p ...

  4. 使用asp.net动态添加html元素

    HtmlGenericControl gen = new HtmlGenericControl("div");        gen.InnerText = "HtmlG ...

  5. PhpCMS标签:专题模块special标签

    专题模块 专题模块PC标签调用说明 模块名:special 模块提供的可用操作 操作名 说明 lists 专题列表 content_list 专题信息列表 hits 专题信息点击排序 下面对所有的操作 ...

  6. Centos 安装KScope1.6.2

    准备工作:安装ctags graphviz,和cscope  (可以用yum install来安装) 1.首先下载kscope,最好下载16.x的版本,这个最好的 下载kscope-1.6.2.tar ...

  7. vim一个快速切换主题的插件(change-colorscheme,原创)

    概述 有时候我们想快速浏览主题并找到一款合适的主题,change-colorscheme将会满足我们的要求. 安装 git https://github.com/chxuan/change-color ...

  8. Android(java)学习笔记87:File类使用

    package cn.itcast_01; import java.io.File; /* * 我们要想实现IO的操作,就必须知道硬盘上文件的表现形式. * 而Java就提供了一个类File供我们使用 ...

  9. javascript编程的最佳实践推荐

    推荐的javascript编程的最佳实践,摘要记录在这里: 可维护的代码保证代码的性能部署代码 1 可维护的代码1.1什么是维护的代码:可理解性——其他人可以接手代码并理解它的意图和一般途径,而无需原 ...

  10. [改善Java代码]异常只为异常服务

    异常原本是正常逻辑的补充,但是有时候会被当做主逻辑使用.看如下代码: public class Client { enum Color { Red, Blue; } public static voi ...