function reference in php

0

I have a problem I can not reference a method that is within the class:

Follow the code below:

define("LOGIN","root");
define("PWD","");
//define("DB", "pj_fatec");
define("DB", "virtual");
define("SERVER","localhost");

class Conexao {

     public static $conn; 

     public function open() {               
           self::$conn = new mysqli(SERVER, LOGIN, PWD, DB);

           // Check connection
           if (self::$conn->connect_error) {
                die("Connection failed: " . self::$conn->connect_error);
           } 

     }
     static function getconexao(){
         if(self::$conn){
             return self::$conn;                    
         }else{
             $this->open();
             return self::$conn;            
        }                
     }      
 }
    
asked by anonymous 21.09.2016 / 18:07

1 answer

0

Hello, the static method should set the static property so that it is visible, so try:

define("LOGIN","root");
define("PWD","");
//define("DB", "pj_fatec");
define("DB", "virtual");
define("SERVER","localhost");

class Conexao {

    public static $conn = null;

    static function getconexao(){
        self::$conn = (self::$conn===null ? new mysqli(SERVER, LOGIN, PWD, DB):self::$conn);
        if (!self::$conn) {
            die("Connection failed: " . var_dump(self::$conn));
        }
        return self::$conn;                         
    }
}
    
21.09.2016 / 18:34