July 23, 2013

How to call a function of a class from other function file?

Mollo’s Question:

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.

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.

Use global keyword inside the function. The variable inside the function will not call values outside its scope.

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

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!