search
Homephp教程php手册php __call方法使用说明

php __call方法使用说明

Jun 13, 2016 am 10:04 AM
phpintroduceInstructions for usestudyarticlemethodhavefriendofneed

本文章来给大家介绍php __call方法使用说明,有需要学习php __call方法的朋友不防进入参考。 相信刚开始接触PHP的童鞋都,在参考书上看到过__call这个魔法方法,而且也看到了它的应用实例!但是,等你真的开始写代码的时候,你用过它吗!?说实话,我用PHP也有一段时间了,但真的从没将它应用到实际开发中。

先说一下__call 的作用吧:"PHP5 对象新增的一个专用方法 ,这个方法用来监视一个对象中的其它方法。如果你试着调用一个对象中不存在的方法,__call 方法将会被自动调用。"

典型用例:

 代码如下 复制代码
runTest('in object context');

 
运行结果:
Calling object method 'runTest' in object context
不过,最近终于看到了一个__call在实际开发中的应用。thinkphp作为国内应用比较普遍的框架,在它里面对这个方法应用个人觉得算的上是完美啊!

比如在thinkphp的:lib->think->core->model.class.php文件里面(Model类)有这么一段代码:

 代码如下 复制代码
    /**
     +----------------------------------------------------------
     * 利用__call方法实现一些特殊的Model方法
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param string $method 方法名称
     * @param array $args 调用参数
     +----------------------------------------------------------
     * @return mixed
     +----------------------------------------------------------
     */
    public function __call($method,$args) {
        if(in_array(strtolower($method),array('field','table','where','order','limit','page','alias','having','group','lock','distinct'),true)) {
            // 连贯操作的实现
            $this->options[strtolower($method)] =   $args[0];
            return $this;
        }elseif(in_array(strtolower($method),array('count','sum','min','max','avg'),true)){
            // 统计查询的实现
            $field =  isset($args[0])?$args[0]:'*';
            return $this->getField(strtoupper($method).'('.$field.') AS tp_'.$method);
        }elseif(strtolower(substr($method,0,5))=='getby') {
            // 根据某个字段获取记录
            $field   =   parse_name(substr($method,5));
            $where[$field] =  $args[0];
            return $this->where($where)->find();
        }else{
            throw_exception(__CLASS__.':'.$method.L('_METHOD_NOT_EXIST_'));
            return;
        }
    }

代码的具体功能,我就不解释了,第一可能我也解释不清楚;第二自己去看一下里面的编程思想还是有很多值的学习的。

说一下他的调用方法,就能看出他的"强大"了。
调用:
$this->dao= M('table');          //快速高性能实例化一个 table 表的模型
$this->dao->field($field)->where($where)->limit($offset . ',' . $limit)->select();    //设置查询字段,查询条件,设置查询数量,最后执行查询操作。当然返回的就是数据库记录了

看到"不对劲"了吧,field方法成对象了,where、limit、select方法也成对象了,其实field、where这些方法在 Model类 里面都不存在。正是因为这些方法不存在,所以这个时候__call方法被执行了,然后又会返回$this对象。所以可以实现这种"衔接"写法,一行代码搞了所有的SQL语句。

不再说下去了,就是向大家介绍一个__call方法的实际应用。要想完全理解它还是建议你自己去看thinkphp的源码吧!(申明:本人跟thinkphp没有任何关系,并不是在鼓吹它的好)

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.