Archive of February 2006
Class variables and static methods in PHP
Found another shortcoming of PHP5's Object Model.
You cannot use class variables to access static methods (or properties). Consider the following...
$myClassVariable = 'simpleClass'; $myInstance = new $myClassVariable;
That works fine, as you would expect from a dynamic language. However the following won't work...
$myClassVariable = 'simpleClass'; $myClassVariable::doSomeStaticClassMethod();
Looking at that, you probably think 'so?, why would I want to do that?'. Well, I have hit this wall twice in the app I am working on. It can always be worked around, but it aint pretty.
I am guessing this is an implementation issue, not a logical one. I can't see any reason why you would want to stop people from using class variables that way. Only possible argument I can think of is that because variables aren't typed, there could be ambiguity trying to interpret it as a class name. I say, not true. The Paamayim Nekudotayim is only used in the context of classes so I still don't see any reason not to have this functionality.
01:18 PM | 3 CommentsPHP5's broken OO model.
Update: My original code listed here was completely broken, meaning that it actually worked (when I didn't want it to). kaintze [at] yahoo.com called me out on it. After his/her comment I fixed the code and changed the wording a bit.
Broken is a bit harsh as this is a known shortcoming and is on the radar for PHP6. In a nutshell, I got bitten by this because I am used to Objective-C which is fully Object Oriented and employs late binding across the board (at least as far as I know).
For anyone playing at home I will try and break this down.
We have two classes: vader and skywalker. Skywalker is naturally a subclass of vade in this example. Now say we have a method in vader called doIt() and another method called quote(). In PHP this would look like this...
class vader
{
public static function doIt()
{
$this->quote();
}
public static function quote()
{
print "Luke, I am your father.";
}
}
now our valiant hero
class skywalker extends vader
{
public static function quote()
{
print "But I wanted to go to Tosche Station to pick up some power converters!";
}
}
So there we have our classes. Now to the meat...
skywalker::doIt();In PHP5 we have the unfortunate situation of Luke Skywalker quoting 'Luke, I am your father.'. Of course Luke is expected to quote: "But I wanted to go to Tosche Station to pick up some power converters!". Sadly, there is no elegant fix for this in PHP5. Like I said, it is coming in PHP6.
If you missed it, what happened was when skywalker invoked its parent's version of doIt() which then invokes quote(), The quote() invocation was bound to the 'vader' implementation even though the object really is a skywalker.
Objective-C and Ruby are two languages I know of that this kind of static polymorphism works for. Java is limited just like PHP. If anyone has any opinions on this be sure to leave them.
11:51 AM | 1 CommentMVC & PHP
As previously mentioned I am working on a medium sized web app written in PHP. It has been an interesting exercise trying to apply structured and consistent design across the app.
After learning Model View Controller through writing desktop apps in Objective-C, trying to apply that same thinking to PHP and HTML was a bit of a mind-shift. Objective-C and Cocoa are designed from the ground up to revolve around MVC as this is the design paradigm that Apple push for that environment. The idea to try and apply the same principles to a web app only came after conversing with a professional web developer who exclusively writes PHP web apps and doesn't touch desktop apps. He mentioned that MVC is the buzzword in the European software engineering market and that developers with MVC experience are sought after. I was amazed to learn that MVC was used for web apps, just as he was surprised to learn that desktop apps can utilise MVC as well. Just goes to show how your background can shape your perception.
So to the point, it took me a little while to work out how to translate the concept of views into a web scripting language. I took a step back and had a good look at my code and I noticed that alot of the 'controller' classes had a LOT of presentation logic in them. This immediately told me something was wrong, and sure enough, the system began to degrade as it was built upon. There just wasn't the flexibility and loose coupling that would be required if this system was to live on for the next 5 - 10 years, which is the plan.
I have just finished re-factoring the core of the application to move to what I feel is a good trade off between complexity and MVC. When I say complexity, that is not entirely true as I think that MVC makes things simpler when you are familiar with the system, but for those looking at it for the first time, it can be hard to see the clear distinctions between M, V and C.
I already had solid Model support through a bunch of classes I wrote that abstract working with Oracle tables very nicely. I took queues from Cocoa's CoreData and Ruby on Rail's persistence layers for this. I am hoping to one day clean this framework up and release it on PEAR to share with other devs.
The idea I used for the concept of views was to create a base class called 'cortecsView' (cortecs is the name of the app) that defined the abstract method display(). This only really started to come together once I started putting ALL html rendering into this system.
Like I said, I have just finished the re-factor but already I can see how much more flexible the system has become and so much easier to maintain. I wonder if there are any good books on using MVC with PHP.
03:27 PM | 0 CommentsNew Town. Good Times.
I have a new address. A few days ago I made the move from the incredibly hot town of Rockhampton to the incredibly hotter town of Townsville.
I would have to say that the move was actually less dramatic than what I was expecting. It is all going really smoothly. I relocated to Townsville to live with my partner of the last however many years. After being apart while I finished studying there and she studied here, I have to say it's good to not have to deal with the un-fun-ness of a long term relationship.
So here's to change. :)
07:27 PM | 1 Comment