views:

95

answers:

5

Hello everyone,

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

A: 

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

Nikkau
but I want an approach wherein I can define filenames be put in a separate file to that their names can be changed easily without any hassles of "finding and replacing"
Gaurav Sharma
You already have that, why the question then :) ?
Jan Hančič
wanted to know if it is a good approach? and if it has any drawbacks.
Gaurav Sharma
+4  A: 

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.

Ciarán Walsh
+1 exactly my thought. Using constants doesn't make much sense
Tomas
A: 

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

jerebear
+3  A: 

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.

Gordon
thanks Mr. Gordon :)
Gaurav Sharma
That's actually my first name, but you're welcome ;)
Gordon
A: 

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.

CaseySoftware
:)thanks for your suggestion. It would very nice of you if you could some resources regarding how to use autoload and implementing a 'standardized include process'
Gaurav Sharma