Is it possible to have two classes with the same name if they're in different folders?
Asked Answered
O

7

10

I was wondering if there is anything wrong with having two classes with the same name in PHP if they're in different sub folders, other than the obvious "human factor" of editing the wrong file by mistake?

I have looked for other posts relating to this, here and elsewhere on the web, but I didn't find any that could answer this specific question. I did however find this Autoload classes from different folders very helpful though, and in fact it solved one of my other questions.

Otis answered 13/4, 2011 at 12:47 Comment(0)
L
12

This is possible to have classes with same name even in same folder.

But Make sure you have loaded only one class in the PHP script at a time.

They can not be loaded in the same script at same time.

PHP does not know if you have created two classes with same name but the fact is PHP will not load them in same script. You can use one class at a time.

You can also look at namespaces in php.

Levitan answered 13/4, 2011 at 12:49 Comment(1)
Thanks I'll look into the namespace thing. And thanks to everyone else who's answered too! In my case I think it unlikely that I will need two classes with the same name, but I thought it wise to find out now rather than down the line if there's likely to be problems!Otis
C
8

That's where namespaces come in. http://www.php.net/manual/en/language.namespaces.rationale.php http://www.php.net/manual/en/language.namespaces.basics.php

This allows you to differentiate between the two classes of the same name.

Celestecelestia answered 13/4, 2011 at 12:52 Comment(0)
S
3

You can use PHP Aliasing: (PHP 5 >= 5.3.0, PHP 7, PHP 8)

use App\Company;
use App\Domain\Company\Models\Company as Tcompany;

https://www.php.net/manual/en/language.namespaces.importing.php

Stribling answered 3/12, 2022 at 12:37 Comment(0)
E
1

Of course you can create the files in the same folder or different folders with the same class names, but you can only use one implementation in one file.

If you really need to give the two classes the same name and must use them in one file, a solution might be namespaces... http://www.php.net/manual/en/language.namespaces.rationale.php

Exuberance answered 13/4, 2011 at 12:53 Comment(0)
W
1

This is possible to have classes with the same name even in the same folder. Here is the sample of code.

file name: namespace.php

<?php
namespace MyProject {

class Connection {
public function __construct(){
    echo 'My Project class call';
    }
}

function connect() {
echo 'My Project connect function.';
}

}

namespace AnotherProject {

class Connection {
public function __construct(){
    echo 'Another Project class call';
    }
}

function connect() {
echo 'Another Project connect function.';
}

}
?>

Another file, where we use this namespace. file name: myapp.php

<?php 

require 'namespace.php';

//create a class object
$obj = new MyProject\Connection;

//calling a function 
MyProject\connect();

//calling a another function
AnotherProject\connect();
?>
Wembley answered 9/7, 2019 at 6:15 Comment(0)
T
-1

In fact you can, but think also about the overloading, and about the interfaces...

Terr answered 13/4, 2011 at 12:52 Comment(0)
P
-2

I believe you will have a conflict when you'll instantiate these classes. Actually I've never tested it, but PHP does not behave like Java, where you can put classes with the same name in different packages, and specify the package to differentiate them upon instantiation...

Parsec answered 13/4, 2011 at 12:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.