Can not declare class Model, because the name is already in use; PHP

0

I'm having this error, but it's not the case that the name is already used. In fact, it was working and continues to work correctly in other parts of the code.

The class Model is an abstract class that connects to the database, the classes that need to connect to the bd extend the class Model .

The error occurs when in the same file I call two methods of different classes, these two classes extend the class Model .

//clase usuario
require_once($CLASES_DIR . 'usuario.class.php');
$usu = New usuario();

$usu->setCorreo($correo);
//metodo de la clase usuario
$id_cl = $usu->id();

//Llamo clase suscripcion
require_once($CLASES_DIR . 'suscripcion.class.php');
$sus = New suscripcion();

The only thing different from the rest of the code where it works is that in the same file I call two classes. How can i fix this?

EDIT: I add the files subscription.class.php and user.class.php

<?php
require('model.php');
class suscripcion extends Model{

private $id;
private $id_usu;
private $t_credito;
private $tipo_sus;
private $fecha_i;
private $fecha_f;

function __construct($id='',$id_usu='',$t_credito='',$tipo_sus='',$fecha_i='',$fecha_f=''){
    //Cargo el constructor de la superclase
    parent::__construct();

    $this->id = $id;
    $this->id_usu = $id_usu;
    $this->t_credito = $t_credito;
    $this->tipo_sus = $tipo_sus;
    $this->fecha_i = $fecha_i;
    $this->fecha_f = $fecha_f;
}
?php
require('model.php');
class usuario extends Model{
private $id;
private $nombre;
private $apellido;
private $correo;
private $contraseña;
private $tel;
private $suscripto; //boolean
private $t_credito;
private $estado;  

function __construct($id='',$nombre='',$apellido='',$correo='',$contraseña='',
$tel='',$suscripto='',$t_credito='',$estado=''){

//Cargo el constructor de la superclase
parent::__construct();

$this->id=$id;
$this->nombre=$nombre;
$this->apellido=$apellido;
$this->correo=$correo;
$this->contraseña=$contraseña;
$this->tel=$tel;
$this->suscripto=$suscripto;
$this->t_credito=$t_credito;
$this->estado=$estado;

}

?php

    abstract class Model{

        protected $_db;


        public function __construct(){
            // Se conecta a la DB al instanciar la clase
            $this->_db = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
            if ( $this->_db->connect_errno ){
                echo "Fallo al conectar a MySQL: ". $this->_db->connect_error;
                return;
            }

            $this->_db->set_charset(DB_CHARSET);

        }

    }
    
asked by Manuel Gancio 21.09.2017 в 01:14
source

2 answers

1

the above partner is right, by invoking two classes:

//clase usuario
require_once($CLASES_DIR . 'usuario.class.php');
$usu = New usuario();

Y:

//Llamo clase suscripcion
require_once($CLASES_DIR . 'suscripcion.class.php');
$sus = New suscripcion();

you are invoking the 'model.php' twice, because in both classes you include them:

 require('model.php');

Remember that both require and include insert code the number of times you invoke or use them regardless of whether you have already called the file before, so when doing a double

 require('model.php');

you get the warning:

 Cannot declare class 'Model', because the name is already in use;
 //Traducción al Español:
 No se puede declarar la clase 'model' , porque el nombre ya está en uso;

Referencing that you have already used the:

  require('model.php');

Remember that in php it becomes a single file when processing and this warning tells you that you are writing the same code twice the solution is easy, when working with PDO, Classes and Objects remember to use:

include_once o require_once 

since these (Quoting the PHP documentation):

  

The require_once statement is identical to require except that PHP will check if the file has already been included and if so, does not include (require) again.

     

The include_once statement includes and evaluates the specified file during the execution of the script. It has a behavior similar to that of the include statement, the only difference being that if the code of the file has already been included, it will not be included again, and include_once will return TRUE. As the name suggests, the file will be included only once.

I invite you to read the documentation, I know it is difficult for time, but believe me, reading the documentation will prevent you from drowning in a glass of water (you will also know the difference between Include and Require: P)!

    
answered by 05.06.2018 в 20:04
0

It has happened to me for example when using wordpress and having to use requires in multiple files to call a class that extends from another. The problem with using a require or include is that PHP interprets it as if all the code were together so if you make a request of an intermediate class that in the file requires MODEL then it is as if declared twice.

    
answered by 21.09.2017 в 06:02