php __call方法使用说明
本文章来给大家介绍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没有任何关系,并不是在鼓吹它的好)

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
