4

Fatal error: Cannot redeclare class My_connection in C:\wamp\www\mvc\smsc\application\models\my_connection.php on line 38

I created a model My_connection. I am calling this method into the constructor of my controller. Every error has meaning. What is the reason behind that error. Please, tell the meaning of error. What can be cause. I have read out the many question relating to this. All questions and their answer somewhat different. They try to correct the code. I want to know the reason. Please, help relative to question.

<?php
class My_connection extends CI_Model{
public function __construct(){
    parent::__construct();
}

public function is_connected(){
    $session_id=$this->session->userdata('session_id');
    $session_username=$this->session->userdata('username');
    $session_password=$this->session->userdata('password');
    $this->db->select('session_id');
    $this->db->from('ci_sessions');
    $this->db->where('session_id',$session_id);
    $ses_query=$this->db->get();
    if($ses_query->num_rows>0){
    foreach($ses_query->rows as $row){
    $session_db_id=$ses_query->row();   
    }
    }
    $this->db->select('username','password');
    $this->db->from('users');
    $this->db->where('username',$session_username);
    $user_query=$this->db->get();
    if($user_query->num_rows()>0){
    foreach($user_query->result() as $row){
    $password=$row->password;
    $username=$row->username;   
    }
    }

    if( md5($password)==$session_password AND $username==$session_username AND  $session_db_id==$session_id){
        return TRUE;
    }
    else {
        return FALSE;
    }`enter code here`
}
}
/**
* End of file my_connection.php
* Location: application/models/my_connection.php
*/`

Last line is 38. According to error. I have no understanding to solve it.

3
  • You can also use a Namespace if you really want to keep the name the same...but changing the name is easiest!
    – Rasclatt
    Dec 10, 2014 at 4:51
  • class My_connection--declared more than once. Dec 10, 2014 at 4:58
  • Problem is solved. Actually, I name the controller and model with same name. I saw this convention in many tutorial and in the documents of CodeIgniter. I name the class and used the prefix My_ for both controller and model with same name. To solve the problem and being familiar with controller and model class. I used this convention Myc_ for controller. Mym_ for model. after that problem is solved. I think, it is stupid solution. But, it work. Dec 10, 2014 at 15:47

2 Answers 2

3

The error message is clear: you can't to declare a new class with a name already used in your app.

I used to put a prefix to my models (eg: My_connection_model) to avoid these errors.

1
  • I used prefix Mym_ for model and Myc_ for controller. It help me to differentiate between controller and model class. Dec 10, 2014 at 15:51
0

I had the above problem. Now, I have solved it. The solution is stupid. But, it work. Actually, the reason to create to such error:

  • when It conflict with its own core classes
  • When, your controller class and model class has same name. While, this is not absolute reason. Because, I run many programs like that. At that time, It show no error. with changing of some configuration, when you try to load. It give error. I solved my error by assigning different names to controller class and model class.

To avoid the problem.

  • use different prefix for controller and model class.Like Myc_ for controller or Mym_ for model.

Note: This is not complete answer. I think, this explanation is not enough to clear the meaning of error. If someone has different solution or meaning to this error. Please, share your solution.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.