29

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?

hakre
  • 193,403
  • 52
  • 435
  • 836
Philippe Gerber
  • 17,457
  • 6
  • 45
  • 40

7 Answers7

18

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

Mike Graf
  • 5,077
  • 4
  • 45
  • 58
  • 1
    Yes, but it adds more details, like where the standard comes from and the method for generating the file names. – Mike Graf Feb 11 '13 at 17:48
  • 2
    This is indeed much better than the accepted answer. A good answer should always provide an explanation. – fuxia Jun 16 '13 at 06:17
  • 7
    The PSR-2 suggests no such thing. In fact, the guide specifically lists that the omission of "Class name prefixes and suffixes" from the guide is intentional. "FooInterface" only appears in a single example, but the guide makes no claims about it's naming. – Riimu Jun 23 '14 at 12:12
  • 1
    https://github.com/php-fig/fig-standards/blob/master/bylaws/002-psr-naming-conventions.md This suggest the prefix suffix route. – ShaunUK Apr 20 '15 at 08:35
  • Is the so-called suggestion part of the linked page? I can not find any file naming suggestions there! – Peyman Mohamadpour Jun 17 '16 at 12:30
  • @trix the answer is 3 years old. IIRC it was a commonly convention at the time for projects moving from Not PSR-2 to it. As you can see from the conversation, it is controversial and rather ambiguous what is the standard. I would suggest that you find a newer PHP project that claims to implement the standard and follow their implementation of the standard. – Mike Graf Jun 17 '16 at 22:48
9

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

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • That was my first idea as well. I for interface and A for abstract. As I code by the ZF coding guidelines and use ZF a lot, I thank you for this interesting link! – Philippe Gerber Jul 19 '09 at 15:28
8

SubpackageAbstract, SubpackageInterface

erenon
  • 18,838
  • 2
  • 61
  • 93
8

I personally would recommend avoiding any usage of Hungarian Notation, and consider following the Java standard for interface names; that is, they're named descriptively just like any other class. See this SO question for a discussion of the ins and outs of Hungarian notation.

A good example of using generic, descriptive names indicative of functionality or behavior can be found in PHP's own SPL, like: "Countable", "Iterator", "ArrayObject".

Community
  • 1
  • 1
jason
  • 8,918
  • 2
  • 37
  • 43
  • 2
    This is the best imho. It should be clear from the name that it is an abstract generic concept instead of a specific implementation. When it comes to abstracts I do like to prefix with 'Abstract' though. – Tim Strijdhorst Sep 22 '16 at 19:28
3

You could also do something like this:

src/Chess/Piece.php

<?php

namespace \Chess;

abstract class Piece implements \Chess\PieceInterface {}

src/Chess/PieceInterface.php:

<?php

namespace \Chess;

interface PieceInterface {}

src/Chess/Piece/Pawn.php:

<?php

namespace \Chess\Piece;

class Pawn extends \Chess\Piece {}

Here's how I would setup autoloading in composer.json

{
    "autoload": {
        "psr-0": {
            "": "src/"
        }
    }
}
Stoutie
  • 1,944
  • 1
  • 21
  • 17
1

Honestly, I believe the Hungarian notation was introduced with C#, because there is not "extends" and "implements" keyword like in Java. So to differentiate the convention became to call it IView. In Java, the interface would be called View alone, and implementations would be called DefaultViewImpl, SmartyViewImpl or something like that. Since, PHP does have extends and implements keywords, it makes sense to use the Java convention. I have heard the argument that the Hungarian notation does lend it self to making the API elements identifiable just by looking at the class names. In that case I would call it either IView or AbstractView.

blockhead
  • 9,655
  • 3
  • 43
  • 69
  • Erm, but like you said, because of `implements` and `interface`, your last point is irrelevant. If I see `class Foo implements Fooer` I don't wonder if `Fooer` is an interface or not. I _know_. Also, you can't use an interface in any other way... Only interfaces can extend other interfaces, you can't instantiate them and you can't use them statically. – jason Jul 19 '09 at 15:53
  • The last point would make sense for somebody who's browsing an API documentation, but not looking at the code. – blockhead Jul 20 '09 at 04:06
  • Hungarian notation was introduced much before C#. It's first major use was with the BCPL language in the 70's. C# in turn was born in 2000. I remember we were using it with Delphi and C++ in the 90's. – Attila Fulop Nov 11 '12 at 10:30
-2

In my opinion, the best way to solve this, is by simply appending Class to your classnames.

namespace Framework\Package\Subpackage;
abstract class AbstractClass {}
Framework/Package/Subpackage/AbstractClass.php

namespace Framework\Package\Subpackage;
interface InterfaceClass {}
Framework/Package/Subpackage/InterfaceClass.php

note that this is still not perfect (however, works perfectly) but i keeps the code similar to the original idea ;)

alexanderpas
  • 2,712
  • 2
  • 23
  • 33
  • 2
    Interfaces are not classes. So why "InterfaceClass"? – Mike Graf Jan 29 '13 at 18:34
  • 2
    A year and half later I understand what @alexanderpas was going for ... Replace "Class" with the underlying classname you're thinking of such as "Class"== "Customer" => AbstractCustomer.php , InterfaceCustomer.php ... Still not ideal IMO, but not nearly bad as it seemed... – Mike Graf Jun 28 '14 at 18:13