Archive for php

Autoloader for WordPress

Posted in Php with tags , , , on April 10, 2013 by andkrup

The process of creating an autoloader for WordPress is a bit problematic. The WordPress core doesn’t use autoloaders, so when you register one via spl_autoload_register(), the autoloader you define in your plugin or theme, will take control of how the WordPress core loads its internal classes.

Other 3rd-party plugins may also break if your autoloader function tries to load their classes in your namespace/source-path.

However, if you put all your classes into your own unique namespace (as is best practice in most other languages), you shouldn’t have any problems in avoiding invocations where your autoload function tries to load classes not from your distribution.

Here I put all my own classes in a namespace called ‘supervillain’, using a plugin structure as pictured here:

Screen Shot 2013-04-10 at 11.42.38

The SoundcloudPlayer-plugin.php file will be included by WordPress. In there I require_once the bootstrap.php, containing this code:

function load($className){
    if(false!==stripos($className, 'supervillainhq')){
        require str_replace("\\", '/', dirname(__FILE__) .'/'. __NAMESPACE__ . $className . '.php');


Further down in Soundcloudplayer-plugin.php I can create an instance of the SoundcloudPlayer class, defined in SoundcloudPlayer.php like this:

$scplayer = new supervillainhq\SoundcloudPlayer();

The SoundcloudPlayer class is defined like this:

namespace supervillainhq{
    class SoundcloudPlayer extends WordPressPlugin{
        public function test(){
            return "test";

Of course, this still needs testing

Launching a separate Firefox instance from Ant, testing php files locally

Posted in Ant, Development with tags , , , on January 17, 2011 by andkrup

Darron Schall has a post about launching Firefox via Apache Ant on mac:

I found the post very useful, although the post was made 3 years ago, but where Darron used the task to test Swf files, I was interested in Launching Firefox in order to test php files locally and this had some unfortunate consequences.

My test-task includes copying all relevant project php-files to the local web servers www-location, then opening Firefox as described in above link. Last, a clean-up task can be invoked to remove the test files.

The problem is that Apache will lock the php-files when you test them because your request will require Apache to execute the php scripts, which means that the clean-up task cannot remove the files, because Apache has a lock on them.

Closing the tab in Firefox doesn’t help – you have to close the browser instance (I assume that Firefox somehow sends a ‘end-session’ signal that will make Apache release its file locks – I don’t really have an understand in this regard, but it works if you close the browser).

But if you do not want to close Firefox because you don’t want to close other tabs, you can combine the tips from this post:

By creating multiple Firefox profiles, you can open as many Firefox instances as you have profiles. This way you can open and close a separate ‘test’ Firefox instance that will allow Apache to release your test files from memory on closing the ‘test’-profile Firefox instance without requiring you to close your other tabs in your normal profile Firefox instance.

The arguments for ‘open’ should look like this:

open -n -a /Applications/ http://localhost/${}/ --args -P developer

, where ‘http://localhost/${}/’ is the web directory that just has been created and enabled for testing by a prior Ant task.

My ‘Test on localhost’ target looks like this:

<target name="Test on localhost" depends="Deploy to localhost">
 <property name="browser" value="open" />
<property name="browser.args" value=" -n -a /Applications/ http://localhost/${}/ --args -P developer" />
 <property name="browser" value="open" />
 <exec executable="${browser}" spawn="yes">
 <arg line="${browser.args}" />

, where the depending task ‘Deploy to localhost’ is the task that copies all files that I want to test to the localhost web directory, and the ‘–args -P developer’ part is the magic that enables Ant to launch Firefox as a separate application instance. The ‘developer’ argument indicates that I have created a new Firefox profile named ‘developer’