Problem instantiating two models in a php controller

Asked

Viewed 62 times

1

I made a mini framework, where the Models extends from a standard class that contains a crud, the only thing the models have is the table information to be used. But when I try to use two models in a controller the first overrides the information in the second.

Controller

namespace Controller;

use Helpers\Seguranca;
use Model\Login;
use Sistema\Controller as CI_controller;

class Usuario extends CI_controller
{

    // Models
    private static $ObjUsuario;
    private static $ObjLogin;


    // Método construtor
    function __construct()
    {
        // Executa o método parente
        parent::__construct();

        // Models
        self::$ObjUsuario = new \Model\Usuario(); // TABELA USUARIO
        self::$ObjLogin = new Login(); // TABALE LOGIN

        // AO TENTAR utilizar o usuario a tabela fica LOGIN

    } // END >> Fun::__construct()
}

MODEL USUARIO

namespace Model;

use Sistema\Database;


class Usuario extends Database
{
    private static $conexao;

    // Método construtor
    public function __construct()
    {
        // Carrega o construtor da class pai
        parent::__construct();

        // Retorna a conexao
        self::$conexao = parent::getConexao();

        // Seta o nome da tablea
        parent::setTable("usuario");

    } // END >> Fun::__construct()

} // END >> Class::Curso

MODEL LOGIN

namespace Model;

use Sistema\Database;


class Login extends Database
{
    private static $conexao;

    // Método construtor
    public function __construct()
    {
        // Carrega o construtor da class pai
        parent::__construct();

        // Retorna a conexao
        self::$conexao = parent::getConexao();

        // Seta o nome da tablea
        parent::setTable("login");

    } // END >> Fun::__construct()

} // END >> Class::Curso

DATABASE CLASS

namespace Sistema;


use \PDO;

class Database
{
    private static $database;
    private static $db;

    private static $table;



    function __construct()
    {
        $database = null;

        // Configurações do Banco de dados
        require("./app/config/database.php");

        // Adiciona as configurações ao item privado
        self::$database = $database;

        try
        {
            // Realiza a conexão do banco
        }
        catch (\PDOException $e)
        {
            echo 'Error:'. $e->getMessage();
        }
    }





    // Método responsável por retornar a conexão
    // com o banco de dados
    public function getConexao()
    {
        return self::$db;
    }


    /**
     *  Métodos para facilitar o desenvolvimento de aplicações
     *  deixando um "CRUD" pré programado
     */


    // Seta a tabela
    public function setTable($table)
    {
        self::$table = $table;
    }

    //......//
}
  • 2

    If you want each class to keep a reference to the table name, the field $table shouldn’t be private static.

  • Should put how? had put only private but could not use it in other functions

  • 1

    You know what the modifiers private and static make - mainly when used with inheritance?

1 answer

0


The problem was in the $table field type. I was setting it as Static switched to private and it worked.

  • Anderson Carlos Woss who helped me with this question

Browser other questions tagged

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