如何管理我的 php 项目中的文件名

发布于 2024-08-16 08:06:58 字数 600 浏览 7 评论 0原文

我的 PHP 项目中的文件名管理有问题。

如果我的项目中有大量 PHP 文件,并且我重命名了 PHP 文件,那么这将导致在我使用该文件的旧名称的所有位置更改文件名。

我已经采用了这种方法的解决方案,但不知道它是否足够有效(想知道是否有其他方法或解决方案可用?)。

方法如下:

  1. 创建一个 php 文件,其中包含如下定义语句:

    define("FILENAME_ADD", "addfeedback.php");
    define("FILENAME_EDIT", "editfeedback.php");
    define("FILENAME_DELETE", "deletefeedback.php");

  2. 将此 php 文件包含在您要访问文件名的每个其他文件中(通常在您创建的每个文件中) .

这种方法足够有效吗?

我正在从事的项目并未完全用 oops 开发,但它使用了一些类,如分页器和会话,它们很好地使用了 oops 概念。

请帮我。

谢谢

I have a problem regarding the filenames management in my PHP project.

If I have a large number of PHP files in my project and I rename a PHP file then this will result in changing the name of file everywhere where I used the old name for that file.

I have adapted a solution to this approach but don't know if it is effecient enough (want to know if there is another approach or solution available?).

The approach is as follows:

  1. create a php file which has define statements like this:

    define("FILENAME_ADD", "addfeedback.php");
    define("FILENAME_EDIT", "editfeedback.php");
    define("FILENAME_DELETE", "deletefeedback.php");

  2. include this php file in every other file where you want to access the filenames (generally in every file that you create).

Is this approach effecient enough ?

The project that I am working on is not fully developed in oops but It uses some of the classes like paginator and session which use oops concepts very well.

Please help me.

Thanks

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

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

发布评论

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

评论(5

情仇皆在手 2024-08-23 08:06:58

我不确定这是否真的有帮助,因为为什么您想要重命名文件而不重命名具有相同名称的常量?您应该采用一种命名方案并坚持下去。

I’m not sure this really helps, as why would you want to rename a file and yet not rename the constant with the same name? You should just adopt a naming scheme and stick with it.

上课铃就是安魂曲 2024-08-23 08:06:58

就像其他地方所说的那样,任何像样的 IDE 都会自动更新路径,最好坚持 命名约定,如 PEAR 并尽可能使用自动加载

使用 define 方法时的主要缺点是,您会到处乱扔常量,并且存在名称与供应商库甚至 PHP 定义的常量发生冲突的风险。如果您确实想在单独的文件中定义所有文件名,请将它们放入一个数组中,例如

<?php // config.inc.php
return array(
    'addFeedback' => '/file/to/add-feedback.php',
    // ...
);

<?php // MyApp.php
    $config = require_once 'config.inc.php';

您的配置也可能驻留在 XML、YAML 或 INI 文件中,但是您必须有某种 Config 类能够读取文件。您经常在当前的框架(ZF、Symfony)中看到这种方法。

除此之外,拥有您的应用程序将要向外界提供的操作的文件名是 FrontController 模式MVC。结合前面提到的命名约定和自动加载,您将不再需要担心包含文件名。

Like said elsewhere, any decent IDE will automatically update paths and it is better to stick to a naming convention, like PEAR and use autoloading if possible.

The main drawback when using the define approach is, you are littering constants all over the place and risk name clashing with constants defined by vendor libs or even PHP. If you really want to define all your file names in a separate file, put them in an array, e.g.

<?php // config.inc.php
return array(
    'addFeedback' => '/file/to/add-feedback.php',
    // ...
);

<?php // MyApp.php
    $config = require_once 'config.inc.php';

Your config might also reside in an XML, YAML or INI file, but then you'd have to have some sort of Config class being able to read the file. You see this approach often in the current frameworks (ZF, Symfony).

Apart from that, having the filenames for the actions your app is about to offer to the outside world is a perfect Use-Case for the FrontController pattern and MVC. Combined with the aforementioned naming convention and autoloading, you won't have to bother about including filenames anymore at all.

不…忘初心 2024-08-23 08:06:58

任何好的 IDE 都会为您完成这项工作,如果您重命名文件,引用将会更新。

Any good IDE will do the job for you, if you rename a file, references will be updated.

你爱我像她 2024-08-23 08:06:58

好吧,另一种选择可能是使用一个“inc.top.php”文件来完成所有包含的操作。

如果您已经包含了一个包含所有常量的文件,为什么不直接将所有文件包含在该文件中并将其包含在每个文件的顶部呢?

我建议从一开始就坚持命名约定,因为它会减少产生“奶酪泡芙时刻”的可能性,在这种情况下,你花了几个小时试图追踪一个错误,结果却变成了“哦......我忘了重命名我的文件!”

Well, another option may be to have a "inc.top.php" file that does all your includes.

If you're already including a file with all the constants, why not just do all the file includes in that file instead and include that at the top of each file?

I would advise sticking to a naming convention from the get-go because it'll reduce the possibility of producing "cheese puff moments" where you spend hours trying to track down a bug that turns about to be "oh...I forgot to rename my file!"

东风软 2024-08-23 08:06:58

为什么不选择命名约定,然后使用自动加载器来处理所有分辨率?甚至一些标准化的包含流程。数以千计的 PHP 应用程序(包括拥有 1000 名开发人员的 WordPress 和 Drupal)都在这样做,只需付出最少的努力并遵守严格的约定。

Why not choose a naming convention and then just use an autoloader to handle all the resolution? Or even some standardized include process. Thousands of PHP applications do it - including WordPress and Drupal with 1000's of developers - with minimal effort combined with strict conventions.

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