PHP Parent, abstract classes and file name conventions

When developing a class library, is there a best practice for naming files?

I thought this should reflect the class hierarchy. 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, under this agreement:

  • Concrete classes that are used solely as the basis for another class have a parent suffix
  • Abstract classes have an abstract paragraph
  • Children's classes start with the name of the class, which it extends.

Is this a good implementation or is there a better way?

+6
source share
4 answers

Naming conventions

There should not be any prefix suffixes in the file names - only the class name so that their autoloader can be found.

Kids classes start with the class name expands.

Never. It is common practice to add (in the class name) words, such as Interface, or Abstract, but not the names of the parents, specifically.

+10
source

There are many different ways you can build your system with regard to class names, automatic loading, interfaces, etc. what you should remember is that if other people use the code while developing on it, you want to make it simple and easy in development, otherwise it will be difficult for developers to code it.

There are many ways, as mentioned above, one of them is PSR-0 , which looks for class names, namespaces and directory structure, it is a simple concept that is used by many major developers such as Zend and many others

Im working on the system at the moment and although it does not have a PSR-0 structure, it is still pretty simple:

You can visit the source here and take a review: https://github.com/AdminSpot/ASFramework

Also, I'm not sure that you will apply the convention to libraries, I mean that the file name has nothing to do with the actual code, so combining the class name with the parent and interfaces is pointless, since you will never include this file through the class name, which he is expanding.

If these are basically the dependencies that you are interested in, an automatic solution to load class dependencies, then I would make a PSR-0 decision.

You basically 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'); 

Create your classes as follows:

 class HTTP_Input extends HTTP_Protocol implements Interface_Readable_Context{} 

and then you donโ€™t need to enable it before hand, as new HTTP_Input will automatically download the following:

  • HTTP / Input.php
  • HTTP / Protocol.php
  • Interface / Read / Context.php
+2
source

There are no rules for naming files, except when you use the framework. However, if you use the framework, you can freely specify your files the way you want, this may cause an error when downloading files.

The best implementation is the one that best suits you when used.

+1
source

It is difficult to answer such a question, because it depends on how you feel good. From my own practice, I can only offer natural, not technical, names. I almost completely refused to use an interface or abstract text in my classes, fell using a short version such as C , I or A in front of the name. Iโ€™m even now trying to prevent this. Give the class the name it deserves;).

There is no problem to figure out if something is an interface or if the class itself is abstract. This is already in the language. Thus, adding to the same class name (and the same for parent classes, however you often call sibling classes) is simply superfluous.

In the end, you just use your classes by their name. Therefore, finding good names instead of technical names is probably the result. The book Clean Code has a good chapter on finding good names for your classes.

 class Classname Classname.php 

Also, use namespaces (or pseudo-spaces as prefixes if you cannot use them because of the PHP version) and put the classes in 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;)

+1
source

Source: https://habr.com/ru/post/891992/


All Articles