Post for the tag: php

Immutability of Data

When dealing with enterprise software, your data is often the most valuable part. It contains all your customer information, contracts, invoices and much more. So what are you going to do to make sure the data is being dealt with correctly? A bug in your code can have a high impact on the integrity of your data. If the bug is causing unwanted changes in your data, fixing the damage might prove to be quite a big challenge.

With this post I would like to show how data immutability can help design a more robust system. One that is less susceptible to bugs that might make unwanted changes to your data.

Service Decoration in Practice

Recently WouterJ has written an excelent article about repositories and how to treat them as collections. In it he also shows that it is useful to have interfaces on your repository classes. If you have not yet read it, I fully recommend doing so.

This concept is something I have recently applied in a project and found that there are some extra benefits in using those interfaces. In this post I would like to expand on WouterJ's ideas and show how this enables service decoration.

Machine Learning: Naive Bayes

Machine learning is becoming more and more ubiquitous in our daily lives. From thermostats which know when you will be home, to smarter cars and the phone we have in our pocket. It seems like it is everywhere and that makes it an interesting field to explore! But what is machine learning? In general terms, it is a way for a system to learn and make predictions. This can be as simple as predicting relevant shopping items to as complex as a digital assistant.

With this blog post I will try to give an introduction into classification using the Naive Bayes classifier algorithm. It is an easy algorithm to implement while giving decent results. But it will need some statistic knowledge to understand, so bear with me. By the end of it you might see some applications and even try to implement it yourself!

Symfony Security, Roles vs. Voters

In my previous blog post I've explained the basics of authentication, authorization and how this is dealt with in Symfony. Due to the size of the post, I've left out several important topics such as roles and voters; Both an equally important part of authentication and authorization. A common misconception is that roles should be used to check permissions. In fact, they should definitely not be used to check permissions directly!

Using bitwise instead of booleans

The naive way of storing many boolean options (in a database) is to create for each option a field and storing a 0 when it is false and 1 when it is true. Consider the following example, something some of you might have written in some variant.

class Config
    public $option1 = false;
    public $option2 = false;
    public $option3 = false;
    public $option4 = false;
    public $option5 = false;
    // ...

$config = new Config();
$config->option1 = true;
$config->option4 = true;
$config->option5 = true;


Which of course works, but adding options will require a new field, which might require creating a compatibility layer for your old data. There is an easier way to do this and it's even more efficient at checking fields.

This brings me to an old topic which I have to explain to all the new people at some point and even once explained not everybody understands how it actually works. So in this post I'm going to explain how to use bitwise operators and how it works internally.

The Basics of Symfony Security

One of the more complex parts of Symfony is probably the Security and everything that comes with it. It's not only rather big, it's also quite flexible with lots of different concepts which often confuse developers. Often enough when developers implement a security system for their website, they call it Authentication or Authorization yet often don't exactly know what they are exactly supposed to call it.

One quote I always refer to is "if you can't explain it simply you don't understand it well enough" and I think it's rather fitting for most cases. I think I've reached a point where I can explain it well enough, so bare with me!

Avoiding static in your code

For anyone not familiar with the static keyword in languages, it is in essence a way of defining things on class level instead of instance level. This allows you to access the same reference from anywhere in your code.

As a new programmer that has been introduced to the static keyword, that might seems very useful. I do not blame you for thinking so, since I did so too not all that long ago. As you write more and more code, you learn and improve and your coding matures. You start to form new ideas as your applications grow bigger and you maintain code that is slowly becoming legacy code. When you look back you will discover that defining things static might make things more difficult than they are worth.

Migrating your project to Symfony

From time to time I see people struggling with trying to port their existing site or web-app to Symfony. Their old framework architecture might not match that of Symfony, making porting their controllers not that easy. Other times, their data structure contained so much logic that it was impossible to simply replace with a new ORM. Thus porting the project can seem like a daunting task.

So, what are your options of migrating to Symfony? Well you could try the 'big bang' approach and just power through your project, porting all of it and then releasing it all at once. While I might not consider this the most optimal way, it does have its advantages. It's gives you a clean slate which allows for a fresh new design and fixing all the legacy stuff you had from the old framework.

But what if your project is too big or you want a more graceful way of migrating? Well there is another way, which allows for a more gradual replacement of the old code. This is running both Symfony and your old project at the same time using a fallback method, and it is a lot easier than you might think.

Decoupling Your Security User

A lot of developers come to #symfony and ask how to implement a login or authentication system. It's quite common to have a bunch of features which require authentication and authorization. While authentication is identifying your user, authorization is granting permissions to this user.

One of the steps of implementing the security features of your application, involves creating an object to contain your user information such as your username, email, password and user id. If you have followed the Symfony docs, you will most likely have ended up with a User entity implementing the Symfony\Component\Security\Core\User\UserInterface. I would like to show you an alternative- decoupled- approach which will prevent several issues within a Symfony application.


I think the need to blog for a programmer is something inherent. It's a desire to spread knowledge, and create a post is something everyone can read. So it's a bit strange, at least to me, that there are not more blogging platforms targeted at developers. A quick Google search either results in Wordpress plugins, NodeJS HTML generators or CMS solutions.

This happened more often if I have to believe this twitter post: I think there are more developers which create their own blogging software and then blog about it, like... This post. Then again, it shows that there is no proper solution for software development blogs yet.

Avoiding Entities in Forms

Being active on IRC, almost every day I see questions coming by regarding forms and entities inside. This does not only give you a headache but it's also risky. You wouldn't want to flush an entity in an invalid state!

"But Using Entities in my Forms is Easy!" Yes, it's certainly easy. You don't have to write any additional code to connect your validation rules and data mapping, not to mention that when it's valid, you only have to flush your entity and you're done. Using this method is especially easy when using CRUD and makes developing applications faster, thus RAD friendly.

Copyright © 2016