开发者

how do I manage filenames in my php project

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 开发者_如何学Cworking 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


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.


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.


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


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!"


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜