Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 months ago.
Improve this questionWhen developing a library of classes, is there a best practice for naming the files?
I was thinking somehow it should reflect the hierarchy of the classes. I was thinking of implementing something like:
Foundation.parent.class.php // Concrete parent class (Class开发者_C百科 with common implementations) File.abstract.class.php // Abstract class (extends Foundation) FileLog.class.php // Child class (extends File)
So within this convention:
- Concrete classes that are solely used as a base for another class have the suffix parent
- Abstract classes have the suffix abstract
- Child classes start with the class name it is extending
Is this a good implementation or is there a better way?
Naming conventions
Names of files should not have any suffixes of prefixes - only name of class, to make it possible find them by autoloader
.
Child classes start with the class name it is extending
Never. It's a common practice to add (into name of class) words like Interface, or Abstract, but not names of parents, definitely.
There are lot's of different ways to construct your system in regards to Class Names, Auto loading, Interfaces etc, what you have to remember is that if other people are going to be using the code, developing on it, you want to make it as simple and easy to learn otherwise developers will have a hard to coding it.
There are many ways as stated above, one of them is the PSR-0 which looks after Class Names, Namespaces and Directory Structure, it's a simple concept that is used by many of the large developers out there such as Zend
and many others
Im working on a system at the moment and although it does not have the PSR-0 structure it is still pretty simple:
You can visit the source here and take a browse: https://github.com/AdminSpot/ASFramework
Also im not sure that you convention is apply able to libraries, I mean the file name has no relevance the the actual code, so combining the class name with parent and interfaces is pointless as your not ever going to include the file via the name of the class it extends.
if it's mainly the dependencies your interested in automatic solution to loading the classes dependencies then i would adopt the PSR-0 solution.
Basically you do the following:
function autoload($className)
{
$className = ltrim($className, '\\');
$fileName = '';
$namespace = '';
if ($lastNsPos = strripos($className, '\\')) {
$namespace = substr($className, 0, $lastNsPos);
$className = substr($className, $lastNsPos + 1);
$fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
}
$fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
require $fileName;
}
spl_autoload_register('autoload');
Structure your classes like so:
class HTTP_Input extends HTTP_Protocol implements Interface_Readable_Context{}
and then you do not need to include before hand, as new HTTP_Input
will auto load the following:
- HTTP/Input.php
- HTTP/Protocol.php
- Interface/Readable/Context.php
It's hard to answer such a question because it depends a lot on what you feel good for yourself. From my own practice I can only suggest to give natural and not technical names. I nearly totally dropped to use Interface or Abstract in my classnames, dropped using a short version like C
, I
or A
in front of the name. I even now try to prevent such. Give the class a name that it deserves ;).
There is no problem to find out if something is an interface or a class itself is abstract. That's already in the language. So additionally putting that into the same classname (and that's a bit the same for parent classes, however you often naturally name child classes related) is just superfluous.
In the end you just use your classes by their name. So find good names instead of technical names is probably the bottom line. There is a nice chapter in the book called Clean Code about finding good names for your classes.
class Classname
Classname.php
Next to that, make use of namespaces (or pseudeo-namspaces in form of prefixes if you can't use them due to the PHP version) and place the classes into subdirectories and use one file = one class.
Interface \File
File.php
class \File\Text
File/Text.php
class \File\PHP
File/PHP.php
Refactor often and review your code ;). Happy coding ;)
I think best thing is PSR-4. Also PSR-0 deprecated.
Before I started developing my first serious project in PHP, I had done a lot of research and realized that the best practice was to follow the general standards (PSR). Of course we are quite free. In some cases, we can bend some parts for our own project.
Every PHP developer must look at PSR.
There are no rules for naming files, except when you use a framework. You can feel free for naming your files just as you want, however, if you are using a framework, it can cause an error when trying to load the files.
The best implementation is the one you feel best when using.
精彩评论