Simple PHP Object Oriented Framework

This marks the initial release of Simple PHP Object Oriented Framework – spoof on Well, I intend for this to be a framework, but I’m starting with the extensible core – the main class library. To that end, the first crack is at:

  • autoload
  • data access components

Autoload is a simple implementation for spl_autoload_register:

  • one class per file
  • \namespace1\subnamespace\MyClass automatically turns into [root_dir]/namespace1/subnamespace/MyClass.php
  • you never have to use require_once again

Database components are a more significant undertaking, as they are designed to be language, connection and execution agnostic.

Finally, currently both PHP 5.2 and 5.3+ are supported. However, I am making a full use of PHP 5.3 namespaces which are not backward compatible. I would expect to continue work on 5.3+ version only.

Please check it out and feedback is more than welcome.

PHP array_merge vs plus union Operator

The purpose of this post is simple: I’ve seen way too many uses of array_merge function with associative arrays in PHP. In most cases the “+” union operator should have been used and here’s why. Given:

$a1 = array('abcd' => 'value 1');
$a2 = array(1234 => 'value 2');

var_export(array_merge($a1, $a2));
var_export($a1 + $a2);

The result is as follows:

array (
  'abcd' => 'value 1',
  0 => 'value 2',
array (
  'abcd' => 'value 1',
  1234 => 'value 2',

I bet the latter is what you would need most of the time. Let’s list the behavior of the two methods:

  • non-matching string keys: simplest case – both methods append all elements and produce merged result
  • matching string keys: array_merge overrides from latter array; union operator keeps element from the former
  • non-matching integer keys: array_merge re-keys latter array(s) as integer keys are available, starting from 0; union operator leaves original keys
  • matching integer keys: array_merge re-keys and appends; union operator keeps element from the former array

Also consider the following:


$key = "1234";
$a = array();

$a[$key] = "abc";
$a["$key"] = "abc";
$a["" . $key] = "abc";
$a[(string)$key] = "abc";


All of the above will result in an integer key! PHP will force anything that looks like an integer into an integer even if you explicitly specify a string. From PHP array documentation:

If a key is the standard representation of an integer, it will be interpreted as such (i.e. “8” will be interpreted as 8, while “08” will be interpreted as “08”).

This means all integer rules will apply when using these elements with array_merge – i.e. all of them will be re-keyed. Conclusion: if you are using associative arrays, you most likely need the “+” union operator to merge arrays and not the array_merge function. Just be mindful of the order in which they are added.

Amazon Finally Releases Android Kindle

It wasn’t a tough call or anything but back in March I did guess that Amazon was planning a Kindle running on Android. Why else would they start their own app store? Now that their strategy is actually taking shape it unsurprisingly presents Amazon-skinned Kindle Fire Android device geared towards its users consuming more Amazon content, services and products. For this purpose, the tablet is very competitively priced – at the time of writing a full $50 less than Nook Color ($199 and $249 respectively).

One can find many reviews of Kindle Fire online. It has also been a frequent comparison subject against Nook Color. Because of the price difference, it compares favorably. However, if I had to pick between the two I would likely go with the Nook Color because of better specs (double RAM, SD card slot, etc.), better support for standards, and just my general discontent for Amazon and its business practices.

Stop Talking about Your ORM

ORM, or object relational mapping, refers to a system of conversion or mapping of different, incompatible types of data in an object oriented framework. For 99.99% of implementations (yes, I pulled that number from wherever I pleased) this is not a good idea. Why? Because most of your Python and PHP scripts mix data access layer with business logic under the guise of ORM.

Stop doing this. Do not tell me how you wrote your own ORM! You are 99.99% and you are wrong. Look back at your “ORM”, then delete it. Then use a pure data access layer that is completely and fully separate from any business logic. Then create your business logic layer on top of it. Do NOT mix them.

Generate Random Characters in PHP

I searched and looked for this but didn’t find a good solution online: I need to come up with a random alphanumeric string of arbitrary length fast and using least memory. How do you do this in PHP? Most solutions I saw posted would define a set of allowed characters, then iterate arbitrary number of times generating a random number each time that would be mapped to one of the characters. Append to result string every time and you get the result.

That method, even when done efficiently, is very expensive because:

  1. You have to generate a different random number with each iteration
  2. Use a run-time interpreted loop
  3. Map to character
  4. Append to string with every iteration

Then I thought of this:

base_convert(mt_rand(1,pow(36,5)), 10, 36);

What does it do?

Step 1: generates a random number between 1 and 36^5. Why 36^5? Because 36 is 26+10: 26 is for letters a-z, and 10 for numbers 0-9. base_convert function’s max base is 36. Why 5? Because for this example, I wanted to generate random string of max 5 characters in length.

Step 2: converts the resulting random number from 10-base to 36-base, resulting in a random alphanumeric string. No for-loops required.

Now, let’s take this one step further. What if we always wanted to get a result that’s n characters long? Well, creatively set a minimum for mt_rand like this:

$n = 10;
base_convert(mt_rand(base_convert(pow(10, $n-1), 36, 10), pow(36, $n)), 10, 36);

This will generate 10 character alphanumeric strings. Enjoy!

Nokia Going WP7 Not a Surprise

Nokia “picking” Windows Phone for their future is not a surprise. It wasn’t to me, anyway. You don’t pick a former Microsoft big head, Stephen Elop, to lead you anywhere else besides Microsoft. In other words, you don’t buy a screwdriver and expect it to hammer nails.

On a side note, that billion dollar cash deal must spell nice bonuses for some executives. At this point, I’m not entirely sure how serious Nokia executives are about this deal anyway. Nokia Windows phone will not be out for at least another year, which is an eternity in mobile world these days; and, in the meantime, they have to hold on to Symbian, which they will soon phase out; and, start over with Windows. It will be like a giant corporation in a startup company mode. One word – disaster.

Amazon Planning Android Kindle?

Amazon it planning to launch an Android tablet device. There is no other way around it. Why? Because there is no reason for Amazon to launch their Android app store otherwise. There is no incentive for them to do favors to Google, or to any of Android tablet manufacturers, which are also Amazon’s competitors, by the way.

The only real reason to establish the Android app store is to create an “ecosystem” (yes, I did use that buzzword) where their next Kindle would naturally fall in place.

Amazon Deletes Your Books

In a Classic Digital Restrictions Management style power exercise, Amazon recently deleted several legally purchased books from customers’ Kindle 2 devices without their knowledge or consent. The delete commands were issued from Amazon’s central systems and faithfully executed by slave devices upon receiving commands. The result was that the users were no longer able to access the books they purchased, as they saw a refund for the prices they paid initially for the purchase.

Amazon later claimed that it turned out they didn’t have the rights to distribute the books, so they felt it was necessary to remove the purchased books from customers’ devices involuntarily. Later they apologized and "promised" they would never do such a thing again.

Where does one start telling what’s wrong with all this? This is wrong on several levels.

First, on the surface, it is wrong that customers can purchase books (or anything for that matter) and later have the seller be able to arbitrarily take it away at their convenience. This does not make any sense with any sales transaction in the physical or the virtual world. For example, you don’t buy the TV at your local electronics retailer, then have the salesperson chase you down the next day, enter your house without your knowledge or consent, take the TV back, leave you a check on the TV stand and write a note saying they didn’t really have the permission from the TV manufacturer to sell that unit yet, so tough luck, it’s been taken back. Those sellers actions would be illegal for several reasons, including breaking and entering, stealing, and violating doctrine of first sale. And guess what – people who point out EULA and small prints – the above would still be illegal even if your TV came with the paper and/or digital copy of the EULA after you unboxed it.

Second, just below the surface, it is wrong that what Amazon did is even remotely considered legal in the USA. Consider if the roles were reversed – a freelancer wrote a piece of software for a large corporation (say, Amazon). The freelancer wrote the software with the backdoor to the company’s server and network that runs the software. Later, arbitrarily without Amazon’s knowledge or consent, the freelancer removed the software and notified Amazon – tough luck, I didn’t really mean to write and give this software for you, here’s your money back if you want it. The freelancer would be in a huge legal trouble. I wonder why it is OK for Amazon to freely be able to do this at their leisure.

And finally, it troubles me that Amazon is able to sell devices with these remotely controlled "features" that are able to manipulate and remove purchased content, and their customers faithfully buying and using these devices knowing those "features" are there. Why is anyone surprised that Amazon exercised the power customers gave them with their purchases of the device and books? Why would they put those remotely controlled "features" in the device if they never intended to use them? Of course they put the time, money and effort into creating, designing and programming them just for these types of scenarios with full intention to use them as they pleased. Is anyone really surprised? By reading news articles, blogs and other online content, a lot of people actually were; which is in itself surprising – Amazon simply used the tool that their customers empowered them with.

And now Amazon is "promising" they will not do that in the future and apologizing for the inconvenience. Some people say that’s the proper thing to do; some say they should give the books back too in addition to the apology. But most seem to be OK with Amazon leaving this feature in these devices and issuing a "promise" not to do it again. OK, great, Mr. Bob from local electronic store. You entered my house, took my TV that I bought from you yesterday without my knowledge or consent. But since you apologized, everything is OK now. In fact, I’ll be buying another TV from you next week. Right? Wrong! That’s ridiculous. If Amazon wants to correct what they did, in addition to all the above they have to unconditionally remove these remotely controlled "features" on the Kindle devices. There should be no more manipulating program/data on users’ devices without their explicit consent (i.e. not some fine print somewhere in the EULA).

I hope that, since this was such a widely publicized event, it will bring more understanding in people’s minds that these types of practices should be avoided. Hopefully not avoided by more legislation, but by understanding and by customers making informed decisions when purchasing products (hear that, Apple?). The matter of the fact is, if you want to own what you buy in the virtual world, the first important step is to demand Free (as in freedom) software, open data/document formats, and absolutely no DRM.