I've started to work with web development using PHP as server side programming language. In that time I didn't know the benefits of object orientation and had a hard time trying to write more maintainable code (for instance, spliting presentation from logic, and etc).

After some years I knew about ASP.NET MVC and I gave it a try. I've learned how to work with the .NET plataform using C# programming language and I really found awesome how better structured my applications could be using good object orientation (dependency injection for instance was something that when I saw it for the first time I thought: "that's everything I always needed").

The point is that although I'm happy working with ASP.NET MVC, I still need in some projects to work with PHP, however I was wondering if it's possible to bring all the benefits from object orientation to PHP. I didn't find a way to do so on my own because for instance, PHP is not strongly typed so that it becomes a little complicated to make use of dependency injection as I've learned when working with ASP.NET MVC. Also, many of the things I've learned when working with MVC in ASP.NET although very cool and useful I didn't find a way to do the same in PHP.

Now here comes the question: does anybody know about resources to learn how to take benefits of all that cool things from OOP to PHP? I'm interested in any resources: books, tutorials, videos, etc.

Thanks very much in advance, and sorry if this question is silly.

  • If someone actually knows this I will be most impressed. – user15279 May 7 '13 at 23:33
  • 6
    Don't try to write C# in PHP. That's as bad an idea as trying to write PHP in C#. If you are interested in writing object oriented PHP, then learn to write object oriented PHP. PHP's support for object orientation is comparable to all other major multi-paradigm languages, and it even has a feature or two that major single-paradigm languages like Java and C# don't (traits, for example). Also, although your question is not silly, the idea that there's a "right way" of doing object orientation is. – yannis May 8 '13 at 4:06
  • @Yannis Rizos: Very balanced comment (+1), also considering the topic. ;-) – Giorgio May 8 '13 at 6:04

Yes, it's quite possible, and it's the way things are going. The PHP community seems to be moving more towards a Java style (heavily object oriented, verbose, etc.).

PHP the Right Way

Some notable things...

  • Namespaced code (after 5.3)
  • Standardized naming / file management as per php standards group (PSR-0)
  • Dependency management with Composer (like Ruby Gems, Node npm modules, etc.)
  • Dependency Injection Container (Symfony's is like Java's Spring), or Pimple, etc.
  • ORM (Doctrine2 is like Java's Hibernate)
  • Automated testing with PHPUnit, Behat, etc.

It's actually quite a nice ecosystem if you are using the newer set of tools.

| improve this answer | |

To develop websites with PHP using an OOP approach you'll need the following.

  1. phpStorm will allow you to apply type details to your objects, methods and variables via PHPDocs. While other IDEs support this, phpStorm has been the only one that seems to work correctly in my experience (NetBeans has just doesn't work). Type details are required to really take advantage of OOP programming, because without it you simply don't know what $obj->doThing() really does.

  2. PHP OOP works best with namespaces and well thought out class loaders. A popular approach is to structure your classes and files in a similar pattern as to Java packages.

  3. What is OOP? This is a very wide open question. There are design patterns, language specific syntaxes and frameworks. It's difficult to say code in PHP is OOP. At what point does it become OOP? C# forces an OOP approach because the web services require it. You can still develop a very badly designed OOP solution in C#, but a very well designed non-OOP approach in PHP. OOP on it's own isn't the solution to anything.

  4. I haven't used it, but I think CodeIgniter uses an OOP approach in it's design. The declarations of controllers, models and views is based upon abstraction. CakePHP is also an OOP approach, but it uses many PHP tricks to shorten the developers work. As a result, it often doesn't follow what I feel is a true OOP design, but it's a very good framework.

  5. PHP lack of typed variables and parameters for methods is one of it's weak points in OOP. It's a problem of scale. OOP is a tool that allows you to scale your problems by building pieces that fit together. What happens when you need to refactor the method of a base class in PHP? The risks of introducing a new bug is very high, where is in C# you'll break the interface contract at compile time. The risk is very low. So designing classes and methods in PHP is a practice in careful forethought, rather than coding time experimenting. If you follow the guidelines for writing PHPDoc comments, declare your parameter types and use phpStorm. You'll be able to handle a larger OOP design then using other tools (in my mind).

| improve this answer | |
  • 2
    Eclipse and various other IDEs handle those features just fine. CodeIgniter / CakePHP are both like 10 years old and (IMO) completely obsolete. Newer frameworks to look at: Symfony2/Silex, Lithium & Laravel. Changing code is easy if you have tests. – Adrian Schneider May 8 '13 at 6:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.