PHP 设计模式 笔记与总结(5)PHP 魔术方法的使用
PHP 魔术方法的使用
① __get/__set:将对象的属性进行接管
当访问一个不存在的对象属性时:
index.php
<?php
define('BASEDIR',__DIR__); //定义根目录常量
include BASEDIR.'/Common/Loader.php';
spl_autoload_register('\\Common\\Loader::autoload'); $obj = new \Common\Object(); //在php中访问一个不存在的对象属性时
echo $obj->title;
会抛出一个错误:Notice: Undefined property: Common\Object::$title in D:\practise\php\design\psr0\index.php on line 9
当在Common/Object.php 中添加 __set 和 __get 方法后
Object.php
<?php
namespace Common; class Object{
function __set($key,$value){
} function __get($key){
}
}
再执行 index.php,不会再报错。
再次修改 Common/Object.php
<?php
namespace Common; class Object{
protected $array = array(); function __set($key,$value){
var_dump(__METHOD__);
$this->array[$key] = $value;
} function __get($key){
var_dump(__METHOD__);
return $this->array[$key];
}
}
index.php
<?php
define('BASEDIR',__DIR__); //定义根目录常量
include BASEDIR.'/Common/Loader.php';
spl_autoload_register('\\Common\\Loader::autoload'); $obj = new \Common\Object(); $obj->title = 'hello';
echo $obj->title;
执行 index.php,页面输出:
string 'Common\Object::__set' (length=20)
string 'Common\Object::__get' (length=20)
hello
② __call/__callStatic:控制 PHP 对象方法的调用(__callStatic 用来控制类的静态方法)
当执行一个不存在的php方法时
index.php:
<?php
define('BASEDIR',__DIR__); //定义根目录常量
include BASEDIR.'/Common/Loader.php';
spl_autoload_register('\\Common\\Loader::autoload'); $obj = new \Common\Object(); //当执行一个不存在的php方法时
$obj->test('hello',123);
执行 index.php 会报一个致命错误:Fatal error: Call to undefined method Common\Object::test() in D:\practise\php\design\psr0\index.php on line 9
如果在 Common/Object 中定义一个__call 方法,则会在方法不存在时自动回调:
<?php
namespace Common; class Object{
function __call($func, $param){ //$func 方法名 $param 参数
var_dump($func, $param);
return "magic function\n"; //返回一个字符串作为返回值
}
}
index.php
<?php
define('BASEDIR',__DIR__); //定义根目录常量
include BASEDIR.'/Common/Loader.php';
spl_autoload_register('\\Common\\Loader::autoload'); $obj = new \Common\Object(); //当执行一个不存在的php方法时
echo $obj->test('hello',123);
页面输出:
string 'test' (length=4)
array
0 => string 'hello' (length=5)
1 => int 123
magic function
当调用一个不存在的静态方法时
Common/Object.php
<?php
namespace Common; class Object{
static function __callStatic($name, $arguments) {
var_dump($name, $arguments);
return "magic function\n"; //返回一个字符串作为返回值
}
}
注意:__callStatic 方法也要声明成静态方法
index.php
<?php
define('BASEDIR',__DIR__); //定义根目录常量
include BASEDIR.'/Common/Loader.php';
spl_autoload_register('\\Common\\Loader::autoload'); //执行一个不存在的静态方法
echo Common\Object::test("hello",1234);
执行 index.php ,页面输出:
string 'test' (length=4)
array
0 => string 'hello' (length=5)
1 => int 1234
magic function
③ __toString:将一个 PHP 对象转换成一个字符串
index.php
<?php
define('BASEDIR',__DIR__); //定义根目录常量
include BASEDIR.'/Common/Loader.php';
spl_autoload_register('\\Common\\Loader::autoload'); $obj = new \Common\Object(); echo $obj;
此时会报错: Catchable fatal error: Object of class Common\Object could not be converted to string in D:\practise\php\design\psr0\index.php on line 8
在 Object.php 中添加 __toString 方法
<?php
namespace Common; class Object{
function __toString() {
return __CLASS__;
}
}
此时再执行 index.php,输出:
Common\Object
④ __invoke:将一个 PHP 对象当成一个函数来执行时,会回调此魔术方法
index.php
<?php
define('BASEDIR',__DIR__); //定义根目录常量
include BASEDIR.'/Common/Loader.php';
spl_autoload_register('\\Common\\Loader::autoload'); $obj = new \Common\Object(); echo $obj("test");
Object.php
<?php
namespace Common; class Object{
function __invoke($param) {
var_dump($param);
return 'invoke';
}
}
页面输出:
string 'test' (length=4)
invoke
PHP 设计模式 笔记与总结(5)PHP 魔术方法的使用的更多相关文章
- php设计模式笔记:单例模式
php设计模式笔记:单例模式 意图: 保证一个类仅有一个实例,并且提供一个全局访问点 单例模式有三个特点: 1.一个类只有一个实例2.它必须自行创建这个实例3.必须自行向整个系统提供这个实例 主要实现 ...
- PHP设计模式笔记二:面向对象 -- Rango韩老师 http://www.imooc.com/learn/236
SPL标准库的使用 SPL是用于解决典型问题(standard problems)的一组接口与类的集合. 1.SPL提供了很多数据结构类,如SplStack.SqlQueue.SqlHeap.SplF ...
- Python学习笔记1:数据模型和特殊方法(魔术方法)
首先不要脸的放上个人网站:www.comingnext.cn 1.关于数据模型 在Python的官方文档中是这样说的: 对象是Python对数据的抽象.Python程序中所有数据都由对象或对象之间的关 ...
- Python 魔术方法笔记
魔术方法总是被__包围, 如__init__ , __len__都是常见的魔术方法,这里主要写一下我遇到的一些魔术方法 setitem 对某个索引值赋值时 即可以进行赋值操作,如 def __seti ...
- 潭州课堂25班:Ph201805201 第十一课 继承,多继承和魔术方法,属性和方法 (课堂笔记)
继承: class p : cls_name = 'p' def __init__(self): print('正在实例化') def __del__(self): print('正在销毁') cla ...
- Python学习笔记之面向对象编程(三)Python类的魔术方法
python类中有一些方法前后都有两个下划线,这类函数统称为魔术方法.这些方法有特殊的用途,有的不需要我们自己定义,有的则通过一些简单的定义可以实现比较神奇的功能 我主要把它们分为三个部分,下文也是分 ...
- php笔记之魔术方法、魔法常量和超全局变量
一.魔术方法(13个)1.__construct()实例化对象时被调用, 当__construct和以类名为函数名的函数同时存在时,__construct将被调用,另一个不被调用.2.__destru ...
- PHP其它常用函数;<<<面向对象(OPP)的三大特性:封装、继承、加态:>>> <----面试题 ;构造方法、析构方法,魔术方法、set、get方法;静态;抽象类;接口
PHP其它常用函数: 赋值:$r->name = "元素"; 取值: echo $r->name; count() 计算数组中的元素数目或对象中 ...
- PHP中的抽象类与抽象方法/静态属性和静态方法/PHP中的单利模式(单态模式)/串行化与反串行化(序列化与反序列化)/约束类型/魔术方法小结
前 言 OOP 学习了好久的PHP,今天来总结一下PHP中的抽象类与抽象方法/静态属性和静态方法/PHP中的单利模式(单态模式)/串行化与反串行化(序列化与反序列化). 1 PHP中的抽象 ...
随机推荐
- JQuery函数库
核心Core 函数$()动态创建由 jQuery 对象包装的 DOM 元素$.unique()去重排序函数$.inArray()在数组中搜索指定的值并返回其索引$.merge()合并数组 属性Para ...
- centos7下yum安装mysql
CentOS 7的yum源中貌似没有正常安装mysql时的mysql-sever文件,需要去官网上下载 # wget http://dev.mysql.com/get/mysql-communit ...
- Codeigniter CRUD代码快速构建
一个与数据库操作打交道的应用,必然涉及到数据的添加.修改.删除等操作.因此CRUD操作几乎成为每个后台管理站点的必备功能.数据库的复杂性,导致PHP操作代码也会有不少的冗余,因此,如果可以有工具自动生 ...
- C# Winform ListView使用
以下内容均来自网上,个人收集整理,具体出处也难确认了,就没写出处了: 一.基本使用: listView.View = View.Details;//设置视图 listView.SmallImageLi ...
- wp8 --未找到与约束ContractName Microsoft.VisualStudio.Text.ITextDocumentFactoryService...匹配的导出
今天打算用VisualStudio2012做一个js效果页面测试的时候,打开VS2012新建项目,但是并没有像之前那样顺利的创建页面,而是弹出了一个错误窗口. 我的系统是win8专业版 64位 ,同时 ...
- A session had already been started - ignoring session_start() 解决方法
A session had already been started - ignoring session_start() --已经存在一个session 打开了 -- 忽略sessioon_star ...
- 从维度理解dp问题
对于dp,我目前的理解就是,干成题目中的那件事需要作出若干次决策,然后你要取其中最优的结果,我们可以用深搜来递归地找最优解,然后我们来观察一下这个递归树的形状,如果它能从底往上直接递推的话,就不用递归 ...
- Android 返回键双击退出程序
/** * 菜单.返回键响应 */ @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == K ...
- uva494 Kindergarten Counting Game
#include<bits/stdc++.h>using namespace std;int main(){ int n=0; char a; int flag=1; while((sca ...
- android 音频采集1
声道数一般表示声音录制时的音源数量或回放时相应的扬声器数量. 假设某通道的音频信号是采样率为8kHz,位宽为16bit,20ms一帧,双通道,则一帧音频数据的大小为: int size = 8000 ...