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 魔术方法的使用的更多相关文章

  1. php设计模式笔记:单例模式

    php设计模式笔记:单例模式 意图: 保证一个类仅有一个实例,并且提供一个全局访问点 单例模式有三个特点: 1.一个类只有一个实例2.它必须自行创建这个实例3.必须自行向整个系统提供这个实例 主要实现 ...

  2. PHP设计模式笔记二:面向对象 -- Rango韩老师 http://www.imooc.com/learn/236

    SPL标准库的使用 SPL是用于解决典型问题(standard problems)的一组接口与类的集合. 1.SPL提供了很多数据结构类,如SplStack.SqlQueue.SqlHeap.SplF ...

  3. Python学习笔记1:数据模型和特殊方法(魔术方法)

    首先不要脸的放上个人网站:www.comingnext.cn 1.关于数据模型 在Python的官方文档中是这样说的: 对象是Python对数据的抽象.Python程序中所有数据都由对象或对象之间的关 ...

  4. Python 魔术方法笔记

    魔术方法总是被__包围, 如__init__ , __len__都是常见的魔术方法,这里主要写一下我遇到的一些魔术方法 setitem 对某个索引值赋值时 即可以进行赋值操作,如 def __seti ...

  5. 潭州课堂25班:Ph201805201 第十一课 继承,多继承和魔术方法,属性和方法 (课堂笔记)

    继承: class p : cls_name = 'p' def __init__(self): print('正在实例化') def __del__(self): print('正在销毁') cla ...

  6. Python学习笔记之面向对象编程(三)Python类的魔术方法

    python类中有一些方法前后都有两个下划线,这类函数统称为魔术方法.这些方法有特殊的用途,有的不需要我们自己定义,有的则通过一些简单的定义可以实现比较神奇的功能 我主要把它们分为三个部分,下文也是分 ...

  7. php笔记之魔术方法、魔法常量和超全局变量

    一.魔术方法(13个)1.__construct()实例化对象时被调用, 当__construct和以类名为函数名的函数同时存在时,__construct将被调用,另一个不被调用.2.__destru ...

  8. PHP其它常用函数;<<<面向对象(OPP)的三大特性:封装、继承、加态:>>> <----面试题 ;构造方法、析构方法,魔术方法、set、get方法;静态;抽象类;接口

    PHP其它常用函数:     赋值:$r->name = "元素";      取值: echo $r->name;  count()   计算数组中的元素数目或对象中 ...

  9. PHP中的抽象类与抽象方法/静态属性和静态方法/PHP中的单利模式(单态模式)/串行化与反串行化(序列化与反序列化)/约束类型/魔术方法小结

      前  言  OOP  学习了好久的PHP,今天来总结一下PHP中的抽象类与抽象方法/静态属性和静态方法/PHP中的单利模式(单态模式)/串行化与反串行化(序列化与反序列化). 1  PHP中的抽象 ...

随机推荐

  1. .net学习之类与对象、new关键字、构造函数、常量和只读变量、枚举、结构、垃圾回收、静态成员、静态类等

    1.类与对象的关系类是对一类事务的统称,是抽象的,不能拿来直接使用,比如汽车,没有具体指哪一辆汽车对象是一个具体存在的,看的见,摸得着的,可以拿来直接使用,比如我家的那辆刚刚买的新汽车,就是具体的对象 ...

  2. ytu 1940:Palindromes _easy version(水题)

    Palindromes _easy version Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 47  Solved: 27[Submit][Statu ...

  3. Start GitHub

    google 的各种被墙大家都懂的,所以转向GitHub 的怀抱. 以前其实也关注过,这回是下决心好好试用了. Fork repositories Forking creates a new, uni ...

  4. oracle11g客户端 安装图解

    软件位置:我的网盘 -- oracle空间 -- oracle工具 -- win64_11gR2_database_clint(客户端) -- 压缩软件包 先将下载下来的ZIP文件解压,并运行setu ...

  5. Ubuntu小点汇总,更新中...

    转自:http://blog.csdn.net/zxz_tsgx/article/details/39713627 昨天重装了Ubuntu14.04 64位版,又被一些基础操作/设置给搞怕了,以前安装 ...

  6. View、ViewGroup (转)

    Android原理揭秘系列之View.ViewGroup (转) Android的UI界面都是由View和ViewGroup及其派生类组合而成的.其中,View是所有UI组件的基类,而ViewGrou ...

  7. WebService站点服务的地址

    天气的地址 http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx

  8. Apache服务器的下载与安装

    关于PHP的运行环境搭载,网上文章繁杂,遂自己整理一篇! PHP的运行必然少不了服务器的支持,何为服务器?通俗讲就是在一台计算机上,安装个服务器软件,这台计算机便可以称之为服务器,服务器软件和计算机本 ...

  9. MATLAB学习笔记(九)——MATLAB符号计算

    (一)符号对象 一.建立符号对象 1.建立符号变量和符号常量(sym,syms): 只可以建立一个符号变量 可以一次性建立多个符号变量 PS:符号常量计算的结果是精确的数学表达式,而数值常量是进行约分 ...

  10. js:数据结构笔记3--栈

    栈是一种特殊的列表,数据结构为LIFO: 定义: function Stack() { this.dataStore = []; this.top = 0; this.push = push; thi ...