PHP Architect's Guide to PHP Design Patterns

Jason E. Sweat

Mentioned 6

Design patterns are comprehensive, well-tested solutions to common problems that developers everywhere encounter each day. Although designed for solving general programming issues, some of them have been successfully adapted to the specific needs of Web development.php architect's Guide to PHP Design Patterns is the first comprehensive guide to the application of design patterns to the PHP development language. Designed to satisfy the need of enterprise-strength development, you will find this book an excellent way to learn about design patterns and an irreplaceable reference for your day-to-day programming.With coverage of more than 16 different types of patterns, including Model-View-Controller, Iterator, MockObject, Register, Proxy, ActiveRecord, DataMapper and many, many others, this book is the ideal resource for your enterprise development with PHP 4 and PHP 5.* Includes over 16 design patterns* Each pattern is discussed in detail with practical code applications* Covers both PHP 4 and PHP 5* Provides a thorough test-driven approach to design patterns* Code is available online

More on

Mentioned in questions and answers.

I need some help from more experienced programmers. I want to improve my MVC skills. But I could not find a good tutorial on Google for MVC. Google always gives "MVC for beginners".

I understand what MVC is and I can make it, but I'm not experienced enough to do something practical in OOP.

If anyone knows a good object-oriented tutorial for MVC, please direct me to the right place — I'm looking for good links, books etc.

Links, that contain PHP-only materials, are marked with php for easier identification.

You cannot even begin to delve into MVC before you have comprehensive understanding of OOP. That include OOP practices (dependency injection, unit testing, refactoring) principles (SOLID, SoC, CQS, LoD) and common patterns (and no, singleton is not an object-oriented pattern).

MVC is an advanced architectural design pattern, which requires solid understanding. It is not meant for beginners or for tiny "hello world" applications. One uses MVC to add additional constraints to the codebase, when simple adherence to OOP practices becomes too loose to control the codebase.

The best I can suggest for you would be to begin by expanding you knowledge regarding object oriented code:

The two lectures above should cover the basics. And then move on to:

When you understand all that was explain in this series, you can expand on:

Also, I would strongly recommend for you to read (in this order):

P.S.: you might also take a look at this book (cautiously, because it has issues): Guide to PHP Design Patterns php

I'm author of YiiMongoDbSuite the extension that brings MongoDB support in ActiveRecord pattern very similar to SQL one already available in core Yii.

My code became quite popular, as it was written with intention to be almost identical as the original Yii approach to the SQL db's for shorten the learning curve, the only two main differences is lack of relation support (as there is not such thing in mongo) and different criteria object.

I found that my code is very fun and easy to use, especially for mongo newcomers that was already used Yii, but I also found/think after few months of developing new features and using my code, that ActiveRecord design pattern is not a good choice for Document oriented DBs and I also think that AR pattern itself is a limiter for mongo true power (ie. new Doctrine has abandoned the AR pattern even in SQL db's).

So my question is what alternative design patterns can be used to "unleash" the full "power" of MongoDB/Document oriented DB's?

Maybe DataMapper pattern is what you need.
This pattern can be realized in different ways with different abilities.
More explanation can be found in this book:

I have known PHP basics, functional programming for a long time before. But this year, in my 2nd semester I have learned Object-Oriented Programming with Java. So I want to apply my knowledge of OOP to PHP. But I find it very difficult to think of any decent utilization. It's not like in Java everything is an object. I see it easier to connect to database an old way, to write some simple functions for logging or registration, etc.

All those examples just gives me knowledge about syntax, not methodology of using it.

So I began to think. Maybe it's because I wanted to apply OOP without any Framework, and it's no use without it? If so - any Framework recommendations that are easy to learn?

OOP has its usage outside of a framework, you cannot compare apples with oranges.

The most respectable frameworks out there are written in OOP. So try to use one such framework in order to see use cases of OOP, then learn from that framework (step through with a debugger and learn how the framework works from the inside).

I would recommend you two things, if you really want to become a pro:

  • Zend Framework - not easy to learn, but it will show you lost of things from the next point:
  • a book about design patterns - VERY VERY IMPORTANT

Regarding the databases: modern PHP code should use PDO.

The framework will only show you usages of design patterns and how to organize your code, so you'll develop a "common sense" for it.

After you've passed that point, you will be able to invent your own OOP-world, without a framework (if this really needs be, although a framework gives you the time to actually be innovative).

I'm developing a web site that (like many other sites) use a bunch of different tools such as php, xml, xsl, json, jquery, css etc.

I'm looking for resources/books that can provide tips on how to use these tools more efficiently. Best practices, tips and tricks and that sort of stuff. For example how to structure files, when to use json instead of xml/xsl, ajax or no ajax and that kind of stuff. Luckily, I don't have to worry about UI design.

Does anyone know any good books/resources that deal with this?

If you are a beginner then consider to start from PHP MySQL Web Development. And if you are intermediate then following are very good references concern on patterns, scalability, performance:

I'm planning on creating a small framework for a dynamic site. When creating a framework is there a standard or code of conduct which should I conclude ?

You can have a look at coding standards, as well as designpatterns (especially the MVC pattern).

Here's a good tutorial which should get you started building your own PHP framework.

A good practice is to look at other PHP frameworks like:

  • Zend Framework
  • Code Igniter
  • Yii
  • Symphony
  • ...

See list here for more PHP frameworks.

I am looking for a tutorial / book that guides me to understand the Controll functions and the best practices to write my own controller + Model

Thanks in advance.

You're question basicaly a design patern question, a realy good book about this subject is:

Architect's Guide to PHP Design Patterns