Reputation: 1465
I'm in the middle of creating a fairly large system that incorporates many different files (about 300 at this point). I've created a file to hold various user-defined functions.
I've begun to think that it would have been best to add a prefix to each function name so as to not have an inadvertent conflict with a previously defined function. For example...
My Function: function add_value()
With Prefix: function PREFIX_add_value()
For the PREFIX_
part I was thinking I could create a variable so the prefix could be easily changed in the future if necessary. So, maybe something like this...
$func_prefix = 'package_name';
function $func_prefix . add_value()
This doesn't work though.
So, is there a best practice about how to name/prepend functions so as to avoid conflicts within a system with many files?
BTW, I have PHP 5.2.17 on my shared hosting plan.
Upvotes: 0
Views: 143
Reputation: 1573
You are trying to solve a problem that need not exist. You are not the first to come across this problem, and many paradigms have been created. This is the world of software design.
I mean this with the best intent, but if you have a project of 300 files, and have asked this question without considering an Object Oriented approach, then the only positive I can give you is that one day you will look back on this project with a slight amount of disbelief.
I would suggest that you learn and Object Oriented programming at a basic level. Java, C++, PHP, it won't matter, but read and experiment. And then consider what 'objects' you can identify in your project.
Namespaces exist to prevent conflict, but don't worry too much about that right now. I suspect that if you build the correct objects in your code , your conflicts may disappear.
That said, the following code, albeit 'ugly', should go towards solving your problem
function file1_foo(){
....
}
$prefix = 'file1_';
$function = $prefix.'foo';
$function();
Upvotes: 1
Reputation: 982
Usage:
<?php
$func_prefix = 'package_name';
$name = $func_prefix . '_add_value';
$name();// call package_name_add_value();
Upvotes: 0