Since PHP 5.x we are encouraged to develop in a more object oriented way. This is why they developed class support for us
. Now to make our web applications even more modular you might consider putting each class in a seperate PHP file, just like Java and C++ developers have been doing for years now. But here is where it becomes tricky, how does PHP know which files to load automatically.
Of course you could include every single file that you might need manually at the top of your index.php. But what if you don't need every single class every time a page is loaded, do you really want to create that kind of an overhead in loading un-needed files? I didn't, so I started looking into the autoloader functionality of PHP. What this feature does is allow you to scan through files in the predefined include directories and attempts to locate the class.
So what I did was build a simple autoloader function to scan each file in predefined directories to find the classes, and if found to stop. To my surprise this was working rather well. All classes that where needed would automatically get loaded by my little autoloader function. However somehow one particular class was not able to be loaded for some reason. The setup was something simple, something like:
All of the classes loaded beautifully, except for class D. For some reason when I tried using it I got exceptions that class C was undefined. When I added some debugging to find out which files where loaded I did see every file for classes A, B and C being loaded, but the actual classes defined inside where not added to PHP. Somehow I have magically created a black whole in PHP that would include files and then forget to create the class definitions internally. To this day I still haven't figured out why PHP refused to store the actual classes in its memory.
The quick fix I applied was ugly and stupid, just manually load the classes and build a big if statement around them to prevent duplicate declarations using the class_exists function.
For my work I recently came across an issue with IE6 and the width of a select. For some reason it does not scale to the width of the content. So I started looking into a custom implementation using JQuery.
Unfortunately the work halted when I ran into serious issues with our beloved website full with frames. But I am continuing it in my spare time as a research project. So soon more on this adventure.
Since I’ve been experimenting a little with WordPress plugin development, I thought it might be useful to write a simple introduction into developing a basic WordPress plugin.
Basic setup of the PHP file for your plugin (which would be called ‘my_plugin/plugin.php’):
// register the plugin activation and de-activation hooks
// you could also register a plugin class, but that's out of scope.
register_activation_hook('my_plugin/plugin.php', 'my_plugin_activate');
register_deactivation_hook('my_plugin/plugin.php', 'my_plugin_deactivate');
function my_plugin_activate()
{
// you might wanna register some data into the settings of WordPress here
$_plugin_settings = array('my_var1' => true, 'my_var2' => 'dingy');
for ( $_plugin_settings as $name => $value )
{
if (!get_option($name))
set_option($name, $value);
}
}
function my_plugin_deactivate()
{
// this is the place to clean-up WordPress, do not erase the settings,
// as you might want to reinitialize the plugin without loosing the users settings.
}
With this done your plugin is able to register the settings that are needed for it to work and clean up after the user is done playing around with it. Keep in mind that you will need to set all settings you need in the activate method, otherwise you might get unexpected exceptions later on.
// register the admin menu for the plugin, you don't need to do this if there are no changable settings
add_action('admin_menu', 'wp_myplugin_admin');
function wp_myplugin_admin()
{
add_options_page('MyPlugin', 'My plugin name',
3, basename(__FILE__), 'wordpress_my_plugin_admin');
}
function wordpress_my_plugin_admin()
{
// simple check to see if the admin settings where posted
if ($_POST['myvar']) {
// deal with the post request (change settings)
}
require_once('admin_menu.php');
}
As you can see the code block above registers an administration section for the plugin. This will enable the plugin to be configured by the end user. The PHP file ‘admin_menu.php’ basically contains the HTML code for the administration page.
You now have the basics setup, the plugin will be called every time a page is requested from WordPress. So all that is left to do is develop the code that will do your magic and you’re done.
A long time ago I posted that I was working on a plugin for WordPress that enabled you to display a optimized version of any WordPress blog for phone or mobile visitors. Though I actually developed the plugin about a year ago I never got around to publishing it. Today is the launch day!!!!
Since I developed the plugin for this site you can see it in action by visiting it with your mobile phone or by visiting the mobile version. Please note that the lay-out is not completely bug free yet, but I’m still working on that.
You can access the WordPress Mobile plugin page under the Plugin section in the header of the blog.
In a previous post a couple of weeks back I stated that I started on developing a UML designer. Mostly because I couldn’t find any recent releases that are free and work decent any more. Since some time has past and a lot of work has been done its time for a quick update.
Initially the idea was to develop the application in C++.Net which was a challenge to say the least. I was disappointed when I couldn’t integrate older C++ libraries I had already written with the .Net application. So as all angry people I abandoned this course of action all together and switched over to C#. Lets just say this put the work back a couple of weeks, as I have no knowledge what so ever on C# development
.
So what have I been up to since this idea popped into my head:
It is unfortunately still way to early to release anything useful that can be played with. Mainly because much of the interface is not even working yet. Even so I have included a screenshot of what it looks like. There is still a lot of work to be done, just a short list would be:
And of course the nice to haves way down the line, all based upon an existing UML design: