如果两个类位于不同的文件夹中,是否可以有两个同名的类?

发布于 2024-10-31 17:19:37 字数 265 浏览 7 评论 0原文

我想知道如果 PHP 中两个同名的类位于不同的子文件夹中,除了错误编辑错误文件的明显“人为因素”之外,是否有任何问题?

我在这里和网络上的其他地方查找了与此相关的其他帖子,但我没有找到任何可以回答这个特定问题的帖子。然而,我确实发现这个从不同文件夹自动加载类非常有帮助,事实上它解决了我的其他问题之一。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

み格子的夏天 2024-11-07 17:19:37

即使在同一文件夹中,也可能存在具有相同名称的类。

但请确保您一次只加载 PHP 脚本中的一个类。

它们不能同时加载到同一个脚本中。

PHP 不知道您是否创建了两个同名的类,但事实上 PHP 不会在同一个脚本中加载它们。您一次可以使用一门课程。

您还可以查看 php 中的 命名空间

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.

揽月 2024-11-07 17:19:37

这就是命名空间发挥作用的地方。
http://www.php.net/manual/en/language.namespaces .rationale.php
http://www.php.net/manual/en/language.namespaces .basics.php

这允许您区分同名的两个类。

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.

不爱素颜 2024-11-07 17:19:37

您可以使用 PHP 别名:
(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

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

踏雪无痕 2024-11-07 17:19:37

当然,您可以在同一文件夹或不同文件夹中创建具有相同类名的文件,但您只能在一个文件中使用一种实现。

如果您确实需要为两个类提供相同的名称并且必须在一个文件中使用它们,则解决方案可能是命名空间... http://www.php.net/manual/en/language.namespaces.rationale.php

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

Hello爱情风 2024-11-07 17:19:37

即使在同一文件夹中也可能存在具有相同名称的类。这是代码示例。

文件名: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.';
}

}
?>

另一个文件,我们在其中使用这个命名空间。
文件名: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();
?>

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();
?>
江南月 2024-11-07 17:19:37

事实上你可以,但也要考虑重载和接口......

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

好倦 2024-11-07 17:19:37

我相信当你实例化这些类时你会遇到冲突。其实我从来没有测试过它,但是PHP的行为不像Java,你可以将同名的类放在不同的包中,并在实例化时指定包来区分它们......

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文