the state pattern

The state pattern becomes useful if your object needs to react differently to different states. Let us assume you are writing an SMTP Proxy. SMTP works in sequences (connection establishment, ehlo/helo, mail, rcpt, data(, …), quit). Each sequence can have different error/success codes which are not available in another. For example you would only return 220 on connection establishment, not 250. And you would return 221 on quit, not 250. So if you are writing a server which works like this, you will most likely use an if then else or switch. The state-pattern would be another method to solve this. However, a more simple example which shows the pros and cons of the state pattern is an activity-based-sleep.
Continue Reading

Swapping values in MySQL without violating constraints

I’ve learned something new! I am synchronizing lots of data coming in XML containers into a MySQL database. Some child nodes of that XML miss identifiers which could be used to update the table and hence I was just truncating the table and re-inserting those values (many to many relation, no id). But since this is a live-used database this lead to the problem that stuff disappeared while using it.

Continue Reading

Contao does not support prepared statements

Due to the naming of methods in Contao’s Database Layer one might get the impression that you might use prepared statements in Contao. Sadly that is not true. The goal of prepared statements is that the Database‘ query compiler does not need to run over and over again for the same query. Due to the method names „prepare“ and „execute“ I assumed (as a bug report shows, other people did as well) that Contao would offer prepared statements.
Continue Reading

Singleton

Somehow I thought the singleton pattern would be pretty useful for my Request and Response classes, because they’re unique things and there should be only one object of them. They shouldn’t get initialized over and over again. PHP is request based, which means, once you got the output of your php script, the request is finished. Reloading the page will just cause a new request. That means if you write a Request or a Response class it doesn’t make much sense to initialize them more than once. At least, that’s my opinion.
Continue Reading

Quality of Regexp

In my last blog-entry you’ve seen that I compared regexp vs nonregexp-techniques and the result was, that there are „bad“ and „good“ regexp; while the good regexp are as fast as non-regexp-techniques. Let’s take a closer look at what „good“ regexp are. Please notice that I’m not a regexp-magician so if you know about a regexp which would do better than the regexp I’ll show, please let me know.

Want to jump directly to the result and skip the boring php examples? 🙂
Continue Reading