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中的抽象 ...
随机推荐
- Linux C 单链表 读取文件 并排序 实例并解释
C的指针挺头疼的,先看一个例子: 给指针赋值和通过指针进行赋值这两种操作的差别确实让人费解.谨记区分的重要方法是:如果对左操作数进行解引用,则修改的是指针所指对象的值: 如果没有使用解引用操作, ...
- linux 小喇叭 没了
Waiting for sound system to respond 方法: # pulseaudio --start -D W: main.c: This program is not inten ...
- linq lambda GroupBy 用法
Linq 中按照多个值进行分组(GroupBy) /// <summary>要查询的对象</summary> class Employee { public int ID ...
- Xamarin.Android开发实践(十一)
Xamarin.Android之使用百度地图起始篇 一.前言 如今跨平台开发层出不穷,而对于.NET而言时下最流行的当然还是 Xamarin,不仅仅能够让我们在熟悉的Vs下利用C#开发,在对原生态类库 ...
- 亿条数据在PHP中实现Mysql数据库分表100张
当数据量猛增的时候,大家都会选择库表散列等等方式去优化数据读写速度.笔者做了一个简单的尝试,1亿条数据,分100张表.具体实现过程如下: 首先创建100张表: $i=0; while($i<=9 ...
- 如何扫描出Android系统媒体库中视频文件
Android系统启动时会去扫描系统文件,并将系统支持的视频文件(mp4,3gp,wmv)扫描到媒体库(MediaStore)中,下面代码演示如何获得这些文件的信息: publicstatic Lis ...
- 浅析C#中的Attribute(转)
最近用到了,所以静下心来找些资料看了一下,终于把这东西搞清楚了. 一.什么是Attribute 先看下面的三段代码: 1.自定义Attribute类:VersionAttribute [Attribu ...
- LogHelper拾遗
1.被简化之前 对已LogHelper,形如: public static void WriteError(string className,string methodName,string mess ...
- POJ1523 SPF(割点模板)
题目求一个无向图的所有割点,并输出删除这些割点后形成几个连通分量.用Tarjan算法: 一遍DFS,构造出一颗深度优先生成树,在原无向图中边分成了两种:树边(生成树上的边)和反祖边(非生成树上的边). ...
- MD5加密(16/32)
public string MD5(string str, int code) { if (code == 16) //16位MD5加密(取32位加密的9~25字符) { return System. ...