This marks the initial release of Simple PHP Object Oriented Framework – spoof on codefly.org. 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:
- 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.
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($a1 + $a2);
The result is as follows:
'abcd' => 'value 1',
0 => 'value 2',
'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.
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.
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.
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:
- You have to generate a different random number with each iteration
- Use a run-time interpreted loop
- Map to character
- 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 “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 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.