您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

PHP 魔术函数 __call()用法_PHP教程

2025/10/30 2:35:58发布21次查看
所谓php的魔术函数,简单的说就是在php中具有特定名称——都是用两个下划线开头的,并且php解释器会在运行到某一个时机的时候自动查找并运行的方法。最常见的魔术函数当然是构造函数方法:__construct了。
在 php 中的方法调用是这样工作的。首先,php 解释器在类上查找方法。如果方法存在,php 就调用它。如果没有,那么就调用类上的魔术函数 __call(如果这个方法存在的话)。如果 __call 失败,就调用父类方法,依此类推。
这样红口白牙的说似乎有点太变态了,我们还是举个例子吧,看如下代码:
 代码如下 复制代码
 class test{
public function __construct(){
echo this is construct!n;
}
}
……
在这个测试类test中,只有个构造函数输出一些可有可无的垃圾字符,别的什么都没有;
这时候我们,把它实例化,并且调用一个*的方法,你猜他会怎么样呢?我们立马就这样做,看着:
 代码如下 复制代码
$send = new test();
$send-> atombomb();
结果是可想而知的,他一定会告诉你没有这个方法的——咱们的确没有这个*的方法!错误信息如下:
debug error: test.php line 9 – call to undefined method test::atombomb()
那么我们把这个类修改一下,加上一个__call方法,看看怎么样呢:
 代码如下 复制代码
……
class test{
public function __construct(){
echo this is construct!n;
}
public function __call($name,$arg){
echo “function name:”,$name,”n arg:”.$arg;
}
}
……
重复上边的调用方式:
$send = new test();
$send-> atombomb(‘ab’,9);
这次看到的结果肯定是和上次不一样的。结果如下:
this is construct! //这个是构造函数自己输出的
//下边这些是__call函数输出的
 代码如下 复制代码
function name:atombomb
arg:array
并且我们也很容易的看出,__call两个参数,第一个参数:调用的方法名,第二个参数:调用方法时候输入的参数(这个地方是个数组)。
说这么多不知道你明白没有,我想你要是明白的话,你一定会问这个东西有个什么鸟用呢?就是我们能够用来干什么呢?
那我给你一个用它的思路吧——学以致用嘛!试想,你如果把一个数据库中的所有表都作为一个个对象,并且对其进行curd操作,你需要写多少个类呢?当然要是你的数组库只有两个表,你完全可以告诉我,只有两个类!但是要是有108个表呢(你例如dede就是108个表),手工输入108个类?显然不科学,21世纪什么最贵?——时间!
我们完全可以写一个类,其余的然他自动创建,我在ibm找了段代码,并且精简了一下,大家可以看看,这个可是高级工程师写的东西啊。
代码如下 复制代码
class dbobject{
private $id = 0;
private $table;
private $fields = array();
function __construct( $table, $fields ) {
$this->table = $table;
foreach( $fields as $key )
$this->fields[ $key ] = null;
}
function __call( $method, $args ) {
if ( preg_match( /set_(.*)/, $method, $found ) ) {
if ( array_key_exists( $found[1], $this->fields ) ) {
$this->fields[ $found[1] ] = $args[0];
return true;
}
}
else if ( preg_match( /get_(.*)/, $method, $found ) ) {
if ( array_key_exists( $found[1], $this->fields ) ) {
return $this->fields[ $found[1] ];
}
}
return false;
}
function insert() {
global $db;
$fields = $this->table._id, ;
$fields .= join( , , array_keys( $this->fields ) );
$inspoints = array( 0 );
foreach( array_keys( $this->fields ) as $field )
$inspoints []= ?;
$inspt = join( , , $inspoints );
$sql = insert into .$this->table. ( $fields ) values ( $inspt );
$values = array();
foreach( array_keys( $this->fields ) as $field )
$values []= $this->fields[ $field ];
$sth = $db->prepare( $sql );
$db->execute( $sth, $values );
$res = $db->query( select last_insert_id() );
$res->fetchinto( $row );
$this->id = $row[0];
return $row[0];
}
//下边删除了3个方法分别是更新update,删除一个,删除全部(战地注)
}
$book = new dbobject( 'book', array( 'author',
'title', 'publisher' ) );
$book->delete_all();
$book->set_title( php hacks );
$book->set_author( jack herrington );
$book->set_publisher( o'reilly );
$id = $book->insert();
echo ( new book id = $idn );
$book->set_title( podcasting hacks );
$book->update();
$book2 = new dbobject( 'book', array( 'author',
'title', 'publisher' ) );
$book2->load( $id );
echo( title = .$book2->get_title().n );
$book2->delete( );……
http://www.bkjia.com/phpjc/629239.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/629239.htmltecharticle所谓php的魔术函数,简单的说就是在php中具有特定名称——都是用两个下划线开头的,并且php解释器会在运行到某一个时机的时候自动查找...
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product