Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Parent, abstract classes and interfaces filename conventions

When 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 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?

like image 489
Globalz Avatar asked Jul 04 '11 23:07

Globalz


3 Answers

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.

like image 61
OZ_ Avatar answered Nov 06 '22 10:11

OZ_


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
like image 32
RobertPitt Avatar answered Nov 06 '22 11:11

RobertPitt


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 ;)

like image 43
hakre Avatar answered Nov 06 '22 11:11

hakre