dsa2c2255888 2013-07-23 22:09
浏览 137
已采纳

如何从其他函数文件中调用类的函数?

I know thats not the best title for my question but I coulnd't come up with a better one (Suggestions are welcome)

Well, this is my situation, I have 3 files:

1) classDBConn.php - In this file is where I connect to the DB and have some functions like this:

class DBConn{
var $conexion;
private $querySql;
var $respuesta;
var $resultado;
function __construct() { 
    $this->conexion = @mysqli_connect('localhost', 'root', 'pass', 'dataBase');
    if(!$this->conexion){
        die('error de conexion('.mysqli_connect_errno().')'.mysqli_connect_error());    
    }
}
function checkBracketGanador($id_torneo){
    $this->querySql = "SELECT COUNT(id_ganador) FROM brackets WHERE id_torneo = '$id_torneo'";
    $this->respuesta = mysqli_query($this->conexion,$this->querySql);
    $this->resultado = mysqli_fetch_array($this->respuesta);
    return $this->resultado[0];
}
// More functions with queries

Note: queries and functions are fine

2)inc_conf.php - In this file is where I create the session and object DBConn. Code:

session_start();
include('classDBConn.php');
$functionsDBConn= new DBConn();
$id_usuario = isset($_SESSION["IDUSUARIO"]) ? $_SESSION["IDUSUARIO"] : false;

3) workingOn.php - In this file is where I make calls to DBConn in order to use those queries. If I do a call like this is just fine:

$res = $functionsDBConn->checkBracketGanador($id_torneo);
echo $res;

But, if I do it inside a function is where everything goes wrong

function test(){
    $res = $functionsDBConn->checkBracketGanador($id_torneo);
    return $res;
}
$a = test();
echo $a;

I keep getting this error:

Fatal error: Call to a member function checkBracketGanador() on a non-object in .../someFolder/workingOn.php on line 67

I've tried making public functions in classDBConn but didn't work

What I'm doing is calling the function outside the function and sending the result as a parameter to the other function but thats exactly what I want to avoid

Any help is appreciate. Thanks in advance.

  • 写回答

2条回答 默认 最新

  • doujizhong8352 2013-07-23 22:30
    关注

    This is to do with scope.

    I assume you instantiate $functionsDBConn = new DBConn(); outside the function at the same level as

    $a = test();
    

    If so you have 2 options

    One:

    function test(){
        global $functionsDBConn;
        $res = $functionsDBConn->checkBracketGanador($id_torneo);
        return $res;
    }
    
    $functionsDBConn = new DBConn();
    $a = test();
    echo $a;
    

    Two:

    function test(&$functionsDBConn){
        $res = $functionsDBConn->checkBracketGanador($id_torneo);
        return $res;
    }
    $functionsDBConn = new DBConn();
    $a = test($functionsDBConn);
    echo $a;
    

    Basically you have to make the object you instantiated visible within the scope of your test() function by either telling the test() function it is available within the global scope global $functionsDBConn; or passing it into the function as a parameter.

    You could also make checkBracketGanador() a static method but lets not get to complex all in a rush.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料