31 Mar 2016

How to get rid of unwanted ifs and elses in your code

Soon or later, when dealing with complex systems you will face a situation when you need to execute a certain logic based on certain conditions. Solving that may bring you to the following mess:

if (object1.isSomething() && object1.hasSomething()){
    new Logic1().execute();
} else if (object1.getSomething() == value1 || object1.getSomething() == value2) {
    new Logic2().execute();
} else if (...) {
    // imagine we repeat this 50 times
    new Logic50().execute();
} else {
   throw new TryAgainException()

As you can see, this can quickly turn out into a nightmare with ifs and elses and a huge file with the code.

Personally, I can’t stand seeing this in my code!

Read More

11 Jan 2015

Tip#3: Enums in PHP or an alternative solution to SplEnum

Many developers from Java or C# background feel lack of enum types in PHP, and I totally understand them, since enums can be very beneficial sometimes. However, Standard PHP Library has an interesting solution to this problem by providing a way to emulate real enumeration objects with “SplEnum” class. This may sound as a great feature, but in some systems it may require an additional installation, since this class is not always available. Read More

09 Jan 2015

When to choose MongoDB over MySQL

MongoDB is a very popular “alternative” to plain old relational database systems such as MySQL, PostgreSQL, MS SQL and etc. It is so popular that many developers build entire projects on it not knowing exactly its advantages and disadvantages. Then they complain that MongoDB is not what they wanted, and it would be better if they didn’t use it at all. Well, any technology is invented for a particular purpose, and there’s no silver bullet; MongoDB is an example. Read More

29 Dec 2014

Advanced “Hydrator” usage in Zend Framework 2

Hydrator is one of the most powerful tools provided by Zend Framwork 2. It becomes extremely helpful when building multi-tier applications where data must be transferred from one layer to another. For example, in my projects, which built based on “Onion Architecture” principles, I use hydrators to restore and save entities into database  in infrastructure layer, and also, I use hydrators in application layer for web services to extract data from the entities and then convert it to json or xml formats. Read More

25 Dec 2014

Setting up multiple entry points for your Zend Framework 2 project

If you ever worked on a large project you most likely faced the situation when you have multiple applications (entry points) consuming the same base code, but each application, however, requires a different set of modules to properly serve requests. For example, you may have a web application for customers, another application for managers, one more application to provide APIs for mobile apps, and lastly, one more application for cron jobs (console application). Each application may also require a separate entry point like for example, index.php, api.php, cron.php and admin.php even though they use the same database, core classes and etc.

In this post, I will show you how to configure apache server to point to the correct index files and how to tell Zend Framework 2 to load the required set of modules depending on the running application. Read More

21 Dec 2014

Onion Architecture and Zend Framework 2

“Onion Architecture” is probably the fanciest and the most discussed architectural design pattern proposed on the Internet  these days. The term was first introduced by Jeffery Palermo back in 2008 in his  series of blogs. The idea behind it is the separation of business logic from its dependencies by placing core modules in the middle and the rest of the code around it. It’s been said a lot about it on the Internet and proposed many different implementations and directory structures. However, most of the proposed solutions are implemented in Java or ASP.Net/C#, and there’s nothing worthwhile in PHP.

In this post,  I want to highlight some important rules of the architecture and introduce the directory structure which I like to have in my PHP projects. Read More