Home > OOP, PHP, platforms and frameworks, softwarearchitecture > The weird static-ness of CakePHP models, or the weird un-static-ness of CakePHP models

The weird static-ness of CakePHP models, or the weird un-static-ness of CakePHP models

October 3rd, 2008

I’ve gotten wrapped up in a few CakePHP projects lately. One thing that’s been bothering me is the confused notion of an instance of a model.

A CakePHP model is really a static entity: the constructor of User (User will be my example model) never returns an object with user data. Find, save, validate, and update operations all take user data as parameters. In fact, the only non-static property of note is the ‘id’ property, which essentially refers to the last record id that instance had to deal with. The ‘id’ property is generally only useful to find out the id of a newly inserted record. Something similar could be achieved by returning the inserted ID from the save() method or even setting a ’static’ class variable. That would be OK in PHP, since the class scope only extends as far as the current request. Because nothing is declared static, you need to instantiate the models. Minor refactoring could obviate the need to create instances.

In general, users (or, broadly, records from the table associated with a model) are dealt with as elements in a result set array/hash, returned from a Find method. I personally wish the elements of the array were instances of User, and that the User model had a real reason not to be static.

As it stands, the the static-ness of CakePHP models leads to very procedural-feeling code. All of this can be overridden, of course, but then it wouldn’t be a CakePHP model anymore; I try not to cut too deep into frameworks, as it will confuse new developers on a project. If CakePHP was old, I’d blame it on PHP 4. But CakePHP is new enough that there isn’t any reason why it couldn’t have a more object-oriented feel. In the meantime, CakePHP should fess up to the static-ness of its models and the documentation should explain them as such.

social bookmark of choice:
  • Digg
  • del.icio.us
  • Ma.gnolia
  • Reddit
  • Slashdot

Greg OOP, PHP, platforms and frameworks, softwarearchitecture , ,

  1. No comments yet.