PHP 魔术方法 __isset __unset (三)
慢慢长寻夜,明月高空挂
__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 (三)的更多相关文章
- PHP 魔术方法之__set__get__unset,__isset,__call
<?php /*** 魔术方法: 是指某些情况下,会自动调用的方法,称为魔术方法 PHP面向对象中,提供了这几个魔术方法, 他们的特点 都是以双下划线__开头的 __construct(), _ ...
- HP叫魔术方法的函数
PHP5.0后,php面向对象提成更多方法,使得php更加的强大!! 一些在PHP叫魔术方法的函数,在这里介绍一下:其实在一般的应用中,我们都需要用到他们!! 1.__construct() 当实例化 ...
- PHP中的魔术方法:__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep, __wakeup, __toString, __set_state, __clone and __autoload
1.__get.__set 这两个方法是为在类和他们的父类中没有声明的属性而设计的: __get( $property ) 当调用一个未定义的属性时访问此方法: __set( $property, $ ...
- __get().__set.__isset,__unset魔术方法
一般来说,总是把类的属性定义为 private .这更符合现实的逻辑. 但是对属性的读取和赋值操作非常频繁的,因此在PHP中,预定义了两魔术方法 "__get()"用来获取私有成员属性值的,只有一个参 ...
- PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep, __wakeup, __toStr
PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep ...
- PHP:__get()、__set()、__isset()、__unset()、__call()、__callStatic()六个魔术方法
哎呀呀,今天小仓鼠学到了魔术方法,简称魔法,哈哈哈哈,神经病啊~ 平时在面试的时候,也会遇到问魔术方法有哪些的问题哦!今天我们来了解一下下~ 1.__get() 形式: __get($objName) ...
- PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep
PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep ...
- PHP中的魔术方法总结:__construct,__destruct ,__call,__callStatic,__get,__set,__isset, __unset ,__sleep,__wakeup,__toString,__set_state,__clone,__autoload
1.__get.__set这两个方法是为在类和他们的父类中没有声明的属性而设计的__get( $property ) 当调用一个未定义的属性时访问此方法__set( $property, $value ...
- __unset()魔术方法 删除类内私有属性
__unset()魔术方法 删除私有属性 unset()对共有属性进行删除 可通过__unset()魔术方法对私有属性进行操作 当在类外部执行unset()函数时,自动执行类内__unset()魔术方 ...
随机推荐
- Keeplived 详解
http://www.cnblogs.com/pricks/p/3822232.html
- cocos2d-x使用tinyxml2存储解析xml
我用的是2.1.4的cocos2d-x,里面自带有tinyxml2库. 导入头文件:#include "support/tinyxml2/tinyxml2.h" using nam ...
- ckfinder动态指定上传路径
默认情况下无法用代码修改config.ascx中的BaseUrl设置,因为其后端代码ConfigFile中并没有提供修改BaseUrl的方法,这里我借用了fckeditor以前的用法:利用sessio ...
- 利用正则表达式作为string.split seprator
某字符串 var str = "{1,att,7},{2,break,7},{3,crit,7},{4,combo,7},{5,break,7},{6,hit,7}"; 需要分割成 ...
- 深入研究Block用weakSelf、strongSelf、@weakify、@strongify解决循环引用(上)
深入研究Block捕获外部变量和__block实现原理 前言 在上篇中,仔细分析了一下Block的实现原理以及__block捕获外部变量的原理.然而实际使用Block过程中,还是会遇到一些问题,比如R ...
- A simple way for hover pop bootstrap nav-menu
.navbar .nav > li .dropdown-menu { margin:; } .navbar .nav > li:hover .dropdown-menu { display ...
- postgresql常用SQL
--查看数据库 select * from pg_database; --查看表空间 select * from pg_tablespace; --查看语言 select * from pg_lang ...
- 使用git ftp发布我个人的hexo博客内容
自己虚拟主机中的博客是由hexo3 + next主题,因为我想将 hexo 编译生成的文件可以通过ftp命令发布到ftp服务器上面. 发布使用的工具是git-ftp: 按照Use Jenkins an ...
- 【转】android应用程序签名
概述 Android系统要求,所有的程序经过数字签名后才能安装.Android系统使用这个证书来识别应用程序的作者,并且建立程序间的信任关系.证书不是用于用户控制哪些程序可以安装.证书不需要授权中心来 ...
- css3 巧用结构性伪类选择器
最近在国外的一个网站上看到的一个关于结构性伪类选择器的用法,觉得十分实用,就自己尝试了一下,并把它给记录下来: 这是最基本的样式: <style type="text/css" ...