PHP: cannot declare class because the name is already in use

PhpOop

Php Problem Overview


I have 5 scripts:

  1. database.php
  2. parent.php
  3. child1.php
  4. child2.php
  5. somescript.php

parent.php class looks like this:

include 'database.php';

class Parent {
    public $db;
    function __construct() {
        $this->db = new Database();
    }
}

The child1.php and child2.php classes looks like this:

include 'parent.php';

class Child1 extends Parent {
    function __construct() {
        parent::__construct();
    }

    function useDb() {
        $this->db->some_db_operation();
    }
}

The problem

When I try to include both child1 and child2 in somescript.php, it returns the following error:

> cannot declare class Database because the name is already in use in > database.php on line 4 (this is the line which contains words 'class Database')

But if I include only a single file (child1 or child2), it works great.

How do I correct that?

Php Solutions


Solution 1 - Php

You want to use include_once() or require_once(). The other option would be to create an additional file with all your class includes in the correct order so they don't need to call includes themselves:

"classes.php"

include 'database.php';
include 'parent.php';
include 'child1.php';
include 'child2.php';

Then you just need:

require_once('classes.php');

Solution 2 - Php

try to use use include_onceor require_once instead of include or require

Solution 3 - Php

Another option to include_once or require_once is to use class autoloading. http://php.net/manual/en/language.oop5.autoload.php

Solution 4 - Php

You should use require_once and include_once. Inside parent.php use

include_once 'database.php';

And inside child1.php and child2.php use

include_once 'parent.php';

Solution 5 - Php

Class Parent cannot be declared because it is PHP reserved keyword so in effect it's already in use

Solution 6 - Php

I had this problem before and to fix this, Just make sure :

  1. You did not create an instance of this class before
  2. If you call this from a class method, make sure the __destruct is set on the class you called from.

My problem (before) :
I had class : Core, Router, Permissions and Render Core include's the Router class, Router then calls Permissions class, then Router __destruct calls the Render class and the error "Cannot declare class because the name is already in use" appeared.

Solution :
I added __destruct on Permission class and the __destruct was empty and it's fixed...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionegorikView Question on Stackoverflow
Solution 1 - PhpM31View Answer on Stackoverflow
Solution 2 - PhpSolaymane ChamaneView Answer on Stackoverflow
Solution 3 - PhpFergal AndrewsView Answer on Stackoverflow
Solution 4 - PhppramView Answer on Stackoverflow
Solution 5 - PhpgentleView Answer on Stackoverflow
Solution 6 - PhpPlanetCloudView Answer on Stackoverflow