通过php魔术函数__call构造简单的数据库访问类DAO

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php
class DAO
{
	private $sqls=array(
			'getActionMoSucc'=>'select * from xxxx where action=$0',
			);

	function __call($method,$args)
	{
		var_dump($method);
		var_dump($args);
		$key=substr($method,3).implode("",$args);
		var_dump($key);
		//if($value=$redis->get($key))
		//      return $value;
		//else
		$this->getFromMysql($method,$args);
	}
	function getFromMysql($key,$args)
	{
		$sql=$this->sqls[$key];
		foreach($args as $k=>$v)
			$sql=str_replace("\$$k",$v,$sql);
		var_dump($sql);
	}
}

$counter=new DAO();

$action="TEST";
$counter->getActionMoSucc($action);
?>