'CodeIgniter Fatal error: Cannot redeclare class

I'm getting the error:

Fatal error: Cannot redeclare class News in application\models\Entities\News.php

The two classes are as follow:

application/models/News_model.php

<?php
require_once(APPPATH."models/Entities/News.php");

class News_model extends CI_Model {

    function __construct() {
        parent::__construct();
    }
}

application/models/Entities/News.php

<?php
use Doctrine\ORM\Mapping as ORM;

/**
* News
*
* @ORM\Table(name="news", indexes={@ORM\Index(name="slug", columns {"slug"})})
* @ORM\Entity
*/
class News {
}

The first class is a model and the last class is entity class for Doctrine.

I cannot see which is the conflict whether the two names are different.

I'm using CodeIgniter 3.1.4.

How can I solve this issue?



Solution 1:[1]

You have 2 classes with the same name.

The file being included with require_once is called News.php, therefor I assume it is a class called News.

Then you also have a News class in Doctrine. There 2 classes are in the same namespace with the same name and php does not allow that. You will either need to look into using namespaces or change the name of one of those 2 classes.

Note that it is not your model itself in codeigniter but the file being included by using require once which is conflicting with your Doctrine News class.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 coderodour