PHP

More features for Extending SimpleXML

Oliver Brown
— This upcoming video may not be available to view yet.

I’ve added two new public static function to [ExtendXML](/exml.txt). The first one is registerTagClass. This takes two arguments, the first is a tag name and the second is a class (that extends ExtendXML. This will make all children of that tag name become that class instead. The second is registerTagCallback. This takes one argument, a callback. That callback must take one argument, a tag name and must return the class (as a string) that those tags should become.

If for example you were parsing HTML and you wanted all paragraph p tags to be handled by ParagraphXML, you could use: ExtendXML::registerTagClass('p', 'ParagraphXML');

A word of warning, it’s case sensitive. If a callback is registered this will be used regardless of any calls to registerTagClass.

If you’ve never used callbacks before, they take one of three forms. Either a string that is the name of a function, an array with the first element a class name (as a string) and the second element a static method of that class (as a string) or finally, as an array with the first element an object and the second element a method of that object.

EXML, ExtendXML, SimpleXML, XML, parsing XML, PHP5, callbacks, programming, coding, parsing HTML, OOP

Overloaded functions?

Oliver Brown
— This upcoming video may not be available to view yet.

Will PHP ever support overloaded functions? I mean properly - I know you can painstakingly fake it using __call.

If I was on a mailing list I’d probably be able to ask someone who could answer.

Ah well.

PHP, programming, coding

Competing with Pimsleur

Oliver Brown
— This upcoming video may not be available to view yet.

As I mentioned earlier, I’ve written the framework for a language learning system similar to Pimsleur. Well the approach is similar but the mechanics are different.

Basically each phrase (in English, the target language, what the instructor says) is a separate audio file. The system then chooses what you listen to automatically handling things like the right amount of repetition or you. How often something is repeated is time based meaning if people do things slowly they will find themselves repeating more often (which makes more sense since slower equates to using the system less often). I almost have a demo ready. The big thing I need however is voice talent. I’ve just about accepted that I will have to suffer by hearing myself but it’s not very clear what’s going on if the instructor, the English voice and the foreign voice are all the same person.

It is also extremely modular. Each module (a module could be a conversation, a sentence, a whole topic - whatever makes sense in the circumstances) has a list of requirements that has to be completed first. This means no explicit ordering of modules is necessary, the system picks whichever one the learner can do. This also allows some cross-language capabilities. If someone learns more than one language and something is similar or the same in the two, it could tell and possibly speed things up depending on how similar they are.

So if there is anyone who might possibly be interested, let me know. If you’re learning a language this could be ideal (during my Maths degree I discovered if I could explain something to someone else, it was a good sign I understood it myself) I’m not quite sure if, and how (or should) this make money but anyone who helps significantly would have a say in what happens with it.

Galactic Horde

Oliver Brown
— This upcoming video may not be available to view yet.

You may have seen the link for Galactic Horde, an online game, in the links bar but never thought to visit. Well the link is not there randomly, it’s something I helped in. Specifically I wrote the PHP back-end for it.

Well I never really had time to play it myself and never knew exactly how the client interacted with what I wrote. The result is that I can read the forums for the game and have no idea what people are talking about. It’s really scary.

Have a look in their forums for an idea.

Wordpress and phpOpenTracker problems…

Oliver Brown
— This upcoming video may not be available to view yet.

Has anyone else tried to use phpOpenTracker with Wordpress? Whenever I do I get strange DB error messages. If you have used the two together, either unsuccessfully or not, please let me know.

Playlists in Windows Media Player

Oliver Brown
— This upcoming video may not be available to view yet.

Windows Media Player will only play .m3u playlists two levels deep…

You can include a playlist inside another playlist and it copes fine. If you include a playlists inside a playlist inside a playlist, the innermost one doesn’t get played.

Why do I tell you this? I decided to try and create a Pimsleur-style CD for Finnish. But to save on effort and increase modularity everything is done in bits and joined together with .m3u files. Since Windows Media Player can’t cope with the nesting though, I had to write a PHP to dump them all in one file (I’m expecting to hit a limit on the amount of songs in a playlist soon).

By the way a .m3u (playlist) is really complicated… it’s a list of URLs separated by newlines…

Code generator update

Oliver Brown
— This upcoming video may not be available to view yet.

The PHP Code Generator now handles docblock style comments properly as well as method keywords. For static, abstract and final keywords, if one is present in any definition of a method it will in the output. For visibility keywords the method will have the “most public” specified. See my previous code generator post for more info.

PHP code generator online

Oliver Brown
— This upcoming video may not be available to view yet.

I’ve just finished a very early version of the PHP code generator I mentioned a while ago. The code for it is rather messy at the moment so I’m not releasing it, but a web form is available to demonstrate it:

PHP Code Generator

At a simple level it takes class fragments you define and includes them in the file. The only fragment defined at the moment is Singleton. To include it you would use:

//cg fragment=Singleton&className=MyClass

Running the generator on just that basically just returns the Singleton fragment with the specified class name. The clever part is it correctly assembles code based on multiple fragments as well as using the code in the file itself. This means if you define MyClass in the file and give it some other methods, these will remain in the generated code.

You can also add code to methods defined in fragments and they still remain, providing you follow a few rules. Only certain fragments will allow code to be added to their methods - they will have //Code Start and //Code End lines. Anything you add between these will remain - all other code will be stripped when the code regenerated.

Issues

Any comments between methods will be removed - with the exception of docblock comments. Comments inside methods are treated like any normal line - they must be inside //Code Start ... //Code End.

At the moment support of visibility keywords and static is a little dodgy. The method should have the properties in it’s first definition.

A methods parameters however will be the same as the last definition. For example the Singleton defines a constructor with no parameters. If you then give it parameters it will keep them.

At the moment the opening brace for a method must be on the same line as the function declaration (i.e. it doesn’t support “one true brace”). This should be easy to fix, but it means methods must be defined like:

function myFunction() { }

A little bit of cleverness

Although I’m not going to reveal exactly how just yet, there is a small bit of markup supported in the fragments allowing bits to be dynamically defined. To see this generate a Singleton and then add a parameter to it’s constructor (and regenerate). The Init method magically has the same parameters.

Finally…

This is very early and very untested with regards to complicated scripts. Any odd behaviour, feel free to let me know. :)

A working Microformats extension to SimpleXML

Oliver Brown
— This upcoming video may not be available to view yet.

I’ve completed a basic version of a class that (sort of) extends SimpleXML. When I say sort of I mean it extends a wrapper class (ExtendXML) that inlcludes all the functionality of SimpleXML.

Download the following to try it out:

The object is created the same way as ExtendXML. After creating it you must also call:

// $mf is a MicroformatXML object 
$mf->mf_init();` 

This adds a new property, $mfHCards, which is an array of hCards. You can access the various hCard values as properties of these objects. As an example:

$mf = MicroformatXML::create\_from\_file($xmlFile);
$mf->mf\_init();
foreach ($mf->mfHCards as $hCard) {
    print $hCard->fn .'  ';
}

Please note this is a very early version that is undocumented and largely untested. It also doesn’t contain every hCard property.

Extending SimpleXML

Oliver Brown
— This upcoming video may not be available to view yet.

I posted a while ago about problems with extending SimpleXML (which I intend to do so I can add automatic microformat support). Well I think I’ve come up with a solution.

I’ve created a class called ExtendXML that is completely extendable (see the other post for an explanation of why SimpleXML isn’t extendable) that provides all of the functionality of SimpleXML.

The initial call is similar to using SimpleXML (except I made the functions static class methods instead):

ExtendXML::load_string($xmlString, [$class, [$simpleClass]]);` `ExtendXML::load_file($xmlFile, [$class, [$simpleClass]]);`

The $class argument contains the name of the class that will be returned. This should extend ExtendXML and defaults to ExtendXML.

The $simpleClass argument contains the name of the SimpleXML class that will be passed to SimpleXML functions. This should extend SimpleXMLElement and defaults to SimpleXMLElement.

The object returned can then be used the same as a SimpleXMLElement object.

I haven’t tested it much or documented it yet, but it supports child tags (returning objects are of the class ExtendXML or whatever you specified), attributes and xpath expressions.

ExtendXML

PHP5, OOP, ArrayAccess, SPL, programming, code, tutorial, microformats, XML, SimpleXML