PHP函数变量里面类似mysql_connect为什么不能赋给变量使用呢?
例如: if($this->dbtype=='mysql'){
$this->oConnect=mysql_connect;
$this->oSelect_db=mysql_select_db;
$this->oQuery=mysql_query;
}elseif($this->dbtype=='mssql'){
$this->oConnect='mssql_connect';
$this->oSelect_db='mssql_select_db';
$this->oQuery='mssql_query';
}
//调用
$this->conn=$this->oConnect($this->dbhost,$this->dbuser,$this->dbpwd) or die("连接服务器失败:".$this->dbhost.':'.$this->dbuser);
例如: if($this->dbtype=='mysql'){
$this->oConnect=mysql_connect;
$this->oSelect_db=mysql_select_db;
$this->oQuery=mysql_query;
}elseif($this->dbtype=='mssql'){
$this->oConnect='mssql_connect';
$this->oSelect_db='mssql_select_db';
$this->oQuery='mssql_query';
}
//调用
$this->conn=$this->oConnect($this->dbhost,$this->dbuser,$this->dbpwd) or die("连接服务器失败:".$this->dbhost.':'.$this->dbuser);
if($this->dbtype=='mysql'){
$this->oConnect='mysql_connect';
$this->oSelect_db='mysql_select_db';
$this->oQuery='mysql_query';
}elseif($this->dbtype=='mssql'){
$this->oConnect='mssql_connect';
$this->oSelect_db='mssql_select_db';
$this->oQuery='mssql_query';
}
//调用
$this->conn=$this->oConnect($this->dbhost,$this->dbuser,$this->dbpwd) or die("连接服务器失败:".$this->dbhost.':'.$this->dbuser);
?>
你想像C语言那样将一个函数的地址赋值给一个函数指针,显然这是不是C语言,PHP中没有这样的用法,php都是弱类型的,将所有的的东西都看为变量和对象来解释,不会给你来个函数的地址。
$oConnect="mssql_connect";
$this->conn=$oConnect($this->dbhost, $this->dbuser, $this->dbpwd) or die("连接服务器失败:".$this->dbhost.':'.$this->dbuser);只是这样就达不到设想了,也没什么意义了
$this->conn = call_user_func_array($this->oConnect,array($this->dbhost,$this->dbuser,$this->dbpwd));
if($this->dbtype=='mysql'){
$this->oConnect='mysql_connect';
$this->oSelect_db='mysql_select_db';
$this->oQuery='mysql_query';
}elseif($this->dbtype=='mssql'){
$this->oConnect='mssql_connect';
$this->oSelect_db='mssql_select_db';
$this->oQuery='mssql_query';
}
//调用
$this->conn = call_user_func($this->oConnect,$this->dbhost,$this->dbuser,$this->dbpwd) or die("连接服务器失败:".$this->dbhost.':'.$this->dbuser);