Class does not connect with PDO to the database and another class in the same path yes

Asked

Viewed 96 times

-1

I’ve got the spread connection:

<?php

namespace classes\conexao;

use PDO;

class Conexao {

    private static $instancia;
    private static $host = "localhost";
    private static $db = "mvc_crud";
    private static $user = "mvc_crud";
    private static $password = "mvc_crud";  

    private function __construct (){}

    private static function obtemConexao() {

        if ( !isset( self::$instancia ) ) {

            try {

                self::$instancia = new \PDO ('mysql:host=' . self::$host . ';dbname=' . self::$db ,  self::$user, self::$password);
                self::$instancia->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
                self::$instancia->setAttribute( PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ );

            } catch ( PDOException $e ) {

                throw new PDOException( "Reveja suas credênciais de conexão ao banco de dados!<br />Contate o administrador!" );

                return;

            }

            return self::$instancia;
        }

    }

    public static function abreConexao () { return self::obtemConexao(); }

}

?> I can’t instantiate it twice

  • I know it’s not recommended this, but what if you test by putting $this->pdo = Conexao::abreConexao(); inside the include function, to see if you’re getting the connection?

  • Your class Conexao is wrong.

  • @Rodrigotognin: Connection::abreConexao(); gives NULL.

  • @Andersoncarloswoss, I think you are not wrong otherwise the other class would not work. It is not no?

  • No. That’s just the mistake: it only works on the first call. If you [Edit] the question, add its code (and remove everything you have now), I can explain/answer.

  • @Andersoncarloswoss, I understood, added at the end of the question the connection class

  • I edited the question by removing all the noise. That’s making a minimum example.

  • 1

    PDO or mysqli? In the connection class you are using mysqli to connect...

  • 1

    You are calling the method abreConexao as if it were static but it is not

  • Please forgive me? I put the wrong class in the question. Now I got the class right there. This is where the error occurs. Can you look at it one more time? @Andersoncarloswoss,

  • Can do the table test of function obtemConexao?

  • @Andersoncarloswoss. I put the vision class (View) at the end of the question. Note that the Pdo class is called a few times and none of them gives this.

Show 7 more comments

1 answer

1


The error is simple: in the method Conexao::obtemConexao, if self::$instancia is defined, the method has no return, so in the first call, when it is not set, it returns correctly, but in the next the return is null.

private static function obtemConexao() {

    if ( !isset( self::$instancia ) ) {

        try {

            self::$instancia = new \PDO ('mysql:host=' . self::$host . ';dbname=' . self::$db ,  self::$user, self::$password);
            self::$instancia->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
            self::$instancia->setAttribute( PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ );

        } catch ( PDOException $e ) {

            throw new PDOException( "Reveja suas credênciais de conexão ao banco de dados!<br />Contate o administrador!" );

            return;

        }

        return self::$instancia;
    }

}
  • then passing self::$instance; out if it will work

  • @Carlosrocha This question should already be marked as solved or closed for some time. What happened?

  • I sincerely apologize, but I’m pretty sure I’ve already marked it as settled and thanked you here. But go ahead. Thanks for the reminder.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.