Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Naming of interfaces/abstract classes in PHP 5.3 (using namespaces)

Prior to PHP 5.3 I used to name interfaces/abstract classes like this:

abstract class Framework_Package_Subpackage_Abstract {} Framework/Package/Subpackage/Abstract.php  interface Framework_Package_Subpackage_Interface {} Framework/Package/Subpackage/Interface.php 

Now with PHP 5.3 and using namespaces I can't use my convention anymore, because interface and abstract are reserved keywords.

namespace Framework\Package\Subpackage; abstract class Abstract {} Framework/Package/Subpackage/Abstract.php  namespace Framework\Package\Subpackage; interface Interface {} Framework/Package/Subpackage/Interface.php 

So, what should I name my classes/interfaces like?

like image 362
Philippe Gerber Avatar asked Jul 19 '09 15:07

Philippe Gerber


People also ask

WHAT IS interface and abstract class in PHP?

Abstract Classes. Interface are similar to abstract classes. The difference between interfaces and abstract classes are: Interfaces cannot have properties, while abstract classes can. All interface methods must be public, while abstract class methods is public or protected.

What do you name an abstract class?

In general, abstract classes follow the same conventions as classes, with one additional rule: abstract class names must end in the term, "Abstract", and that term must not be preceded by an underscore.

What is the use of namespace in PHP?

A namespace is used to avoid conflicting definitions and introduce more flexibility and organization in the code base. Just like directories, namespace can contain a hierarchy know as subnamespaces. PHP uses the backslash as its namespace separator.

Does PHP support namespace?

In the PHP world, namespaces are designed to solve two problems that authors of libraries and applications encounter when creating re-usable code elements such as classes or functions: Name collisions between code you create, and internal PHP classes/functions/constants or third-party classes/functions/constants.


2 Answers

A current coding guide "PSR-2" basically suggests you drop the interface down one directory and combine the name.

eg:

File \Vendor\Foo\Interface.php ===> \Vendor\FooInterface.php. 

and the use stament eg:

use \Vendor\Foo\Interface; ===> use\Vendor\FooInterface; 

see: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md

like image 161
Mike Graf Avatar answered Oct 02 '22 15:10

Mike Graf


About this question (Abstract and Interface), you might have a look at the post "Migrating OOP Libraries and Frameworks to PHP 5.3" on Matthew Weier O'Phinney's blog -- it's about Zend Framework, and how they could solve that problem in 2.0.

One of the things they note is :

In other OOP languages, such as Python, C#, interfaces are denoted by prefixing the interface with a capital 'I'; in the example above, we would then have Zend::View::IView.

So, in your example, you would have something like this, I guess :

namespace Framework\Package\Subpackage; abstract class ASubpackage {} Framework/Package/Subpackage/ASubpackage.php  namespace Framework\Package\Subpackage; interface ISubpackage {} Framework/Package/Subpackage/ISubpackage.php 

What do you think about that ? (I have not tested this way, but it doesn't look like a bad idea ? )

like image 38
Pascal MARTIN Avatar answered Oct 02 '22 15:10

Pascal MARTIN