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. jquery require.js AMD

    一.为什么要用require.js? 最早的时候,所有Javascript代码都写在一个文件里面,只要加载这一个文件就够了.后来,代码越来越多,一个文件不够了,必须分成多个文件,依次加载.下面的网页代 ...

  2. Linux命令自己总结

    对于每一个Linux学习者来说,了解Linux文件系统的目录结构,是学好Linux的至关重要的一步.,深入了解linux文件目录结构的标准和每个目录的详细功能,对于我们用好linux系统只管重要,下面 ...

  3. ytu 1041: 迭代法求平方根(水题)

    1041: 迭代法求平方根 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 227  Solved: 146[Submit][Status][Web Bo ...

  4. Android Inflate

    inflate就相当于将一个xml中定义的布局找出来. 三种方式可以生成LayoutInflater: LayoutInflaterinflater=LayoutInflater.from(this) ...

  5. 电赛菜鸟营培训(二)——STM32F103CB之中断控制

    一.中断的概念 中断可以理解为一个有激励给它就会立马给你响应的一个东东.就是说在你执行main函数的内容时,当有这个触发时便会跳转到这个中断内存,执行这段代码. Attention: 1.中断的用法, ...

  6. 【练习】使用接口回调和handler实现数据加载到listview

    代码结构 布局: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xml ...

  7. php生成二维码的插件phpqrcode

    参考网址: http://www.thinkphp.cn/topic/7749.html http://blog.csdn.net/stxyc/article/details/44650971 php ...

  8. filter()函数 条件筛选

    filter()函数 filter()函数是 Python 内置的另一个有用的高阶函数,filter()函数接收一个函数 f 和一个list,这个函数 f 的作用是对每个元素进行判断,返回 True或 ...

  9. SU Demos-04Deconvolution-01FX

    先看readme, 运行结果,

  10. win 7 普通家庭版 装IIS

    每当一个程序员入职的时候,几乎都会干一件事情,就是重装操作系统,这是一场不易之战: 1)耗时太长: 2)容易遇到怪异的系统行为. 1.win7为毛装不上VS2012 先装一个win7 x64 旗舰版, ...