php - How to call a function of a class from other function file? -
i know thats not best title question coulnd't come better 1 (suggestions welcome)
well, situation, have 3 files:
1) classdbconn.php - in file connect db , have functions 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) brackets 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 queries
note: queries , functions fine
2)inc_conf.php - in file create session , object dbconn. code:
session_start(); include('classdbconn.php'); $functionsdbconn= new dbconn(); $id_usuario = isset($_session["idusuario"]) ? $_session["idusuario"] : false;
3) workingon.php - in file make calls dbconn in order use queries. if call fine:
$res = $functionsdbconn->checkbracketganador($id_torneo); echo $res;
but, if inside function goes wrong
function test(){ $res = $functionsdbconn->checkbracketganador($id_torneo); return $res; } $a = test(); echo $a;
i keep getting error:
fatal error: call member function checkbracketganador() on non-object in .../somefolder/workingon.php on line 67
i've tried making public functions in classdbconn didn't work
what i'm doing calling function outside function , sending result parameter other function thats want avoid
any appreciate. in advance.
this scope.
i assume instantiate $functionsdbconn = new dbconn();
outside function @ same level as
$a = test();
if 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 have make object instantiated visible within scope of test() function either telling test() function available within global scope global $functionsdbconn;
or passing function parameter.
you make checkbracketganador() static method lets not complex in rush.
Comments
Post a Comment