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);

解决方案 »

  1.   

    <?php 
    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);
    ?>
      

  2.   

    本帖最后由 xuzuning 于 2010-04-18 11:41:59 编辑
      

  3.   

    请问这样做有什么意义呢?
    你想像C语言那样将一个函数的地址赋值给一个函数指针,显然这是不是C语言,PHP中没有这样的用法,php都是弱类型的,将所有的的东西都看为变量和对象来解释,不会给你来个函数的地址。
      

  4.   

    自定义的函数可以赋给一个变量,内部函数怎么就不行了呢
    $oConnect="mssql_connect";
    $this->conn=$oConnect($this->dbhost, $this->dbuser, $this->dbpwd) or die("连接服务器失败:".$this->dbhost.':'.$this->dbuser);只是这样就达不到设想了,也没什么意义了
      

  5.   

    类里面会默认你调用oConnect方法,如果一定要那么写可以使用call_user_func_array。
    $this->conn = call_user_func_array($this->oConnect,array($this->dbhost,$this->dbuser,$this->dbpwd));
      

  6.   


    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);
      

  7.   

    已经解决了,谢谢各位。本想自己写个兼容多种数据库的后台的,不过发现sql语法、分页都不同,所以估计会以后再说了。请问用adodb类库的话有没有必要呢?