【很变态】PHP类实例化对象竟然可以访问类的“静态(static)方法”!!!
2016-07-14 11:58
861 查看
之前发现一个PHP的变态问题:PHP中静态(static)调用非静态方法详解
这次看了下 ThinkPHP 的源码 function.inc.php ,里面有个函数:
该函数注释说可以 支持调用类的静态方法,从源码表面看,按理说类实例是不能调用类的静态方法。可是呢,PHP偏偏就支持 类实例化对象可以访问静态(static)方法,但是不能访问静态属性。
参考资料:http://php.net/manual/zh/language.oop5.static.php
这次看了下 ThinkPHP 的源码 function.inc.php ,里面有个函数:
/** * 取得对象实例 支持调用类的静态方法 * * @param string $name 类名 * @param string $method 方法 * @param string $args 参数 * @return object 对象实例 */ function get_instance_of($name, $method = '', $args = array()) { static $_instance = array(); $identify = empty($args) ? $name . $method : $name . $method . to_guid_string($args); if (!isset($_instance[$identify])) { if (class_exists($name)) { $o = new $name(); if (method_exists($o, $method)) { if (!empty($args)) { $_instance[$identify] = call_user_func_array(array(&$o, $method), $args); } else { $_instance[$identify] = $o->$method(); } } else { $_instance[$identify] = $o; } } else { halt(L('_CLASS_NOT_EXIST_') . ':' . $name); } } return $_instance[$identify]; }
该函数注释说可以 支持调用类的静态方法,从源码表面看,按理说类实例是不能调用类的静态方法。可是呢,PHP偏偏就支持 类实例化对象可以访问静态(static)方法,但是不能访问静态属性。
<?php ini_set('display_error', true); error_reporting(E_ALL); class Dog { public static $name = 'wangwang'; static function say($data) { echo $data; } } $myDog = new Dog(); $myDog->say('123456'); // 输出 123456 echo $myDog->name; // 发出Notice信息: Undefined property: Dog::$name in ... ?>
参考资料:http://php.net/manual/zh/language.oop5.static.php
相关文章推荐
- 【转】记录PHP、MySQL在高并发场景下产生的一次事故
- PHP 操作 MySQL 执行数据库事务
- PHP返回32位与16位的md5加密值
- PhpStorm 8.x/9.x 快捷键设置/个性化设置,如何多项目共存?如何更换主题?
- Yii2使用Cookie的注意事项
- Yii中srbac权限扩展模块工作原理与用法分析
- php中curl同时发送多个请求curl_multi函数集的用法
- PHP工程师知识体系结构
- vsftp 根据用户设置
- PHP特性01:cURL实现get和post,在url间做数据交互;
- PHP 设置考试计划
- Page分页
- yii2权限管理
- Yii视图操作之自定义分页实现方法
- PHP5.3配置文件php.ini-development和php.ini-production的区别
- Yii视图CGridView实现操作按钮定义地址示例
- PHP Smarty模板基础
- phpMyAdmin提示:配置文件权限错误,无法写入!解决方法
- yii2.0 StringHelper
- PHP输出XML文件函数