Polymorphic Extendable Base Class

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Polymorphic Extendable Base Class

Post by Benjamin »

Not sure what to call this here.

The symfony framework creates extendable base classes which represent records from database tables. These classes consist of getters and setters for each data as well as crud methods.

The framework generates these classes from yaml files, writes them to disk and it works well.

I'm pondering whether it's possible to generate these classes on the fly (or create a class that serves the same purpose) based on the database schema (perhaps from a cached db scheme file, or even an auto generated PHP file).

This is theoretical at this point, and I think it can be done. Just want to hear what you guys think before I start trying to write it.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Polymorphic Extendable Base Class

Post by Christopher »

Sounds like either RowDataGateway or ActiveRecord. Both are very generate-able. My guess is that it is ActiveRecord as that is catnip for frameworks.
(#10850)
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: Polymorphic Extendable Base Class

Post by allspiritseve »

arborint wrote:My guess is that it is ActiveRecord as that is catnip for frameworks
:D
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Polymorphic Extendable Base Class

Post by Benjamin »

I should mention that the goal is to be able to override the methods in the ghost class by coding classes that extend it.

Code: Select all

class user extends system {
    protected function setUserName($var) {
        parent::setUserName($var);
    }
}
The class system won't contain any methods itself. It would add relevant methods to itself depending on what extends it. Yes, I am that crazy. How close can I get to accomplishing this?
Tobey
Forum Newbie
Posts: 12
Joined: Thu May 14, 2009 11:40 am
Location: Germany

Re: Polymorphic Extendable Base Class

Post by Tobey »

Hey!
You should take a look at Magic Functions, especially __call.
:)
Last edited by Tobey on Fri Jun 12, 2009 8:04 am, edited 1 time in total.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Polymorphic Extendable Base Class

Post by Benjamin »

Tobey wrote:Hey!
You should take a look at Magic Funtions, especially __call.
:)
Thanks Tobey, that's definitely going to be apart of it.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: Polymorphic Extendable Base Class

Post by Luke »

I don't get it. What do you mean? :?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Polymorphic Extendable Base Class

Post by Benjamin »

For demonstration purposes lets say I have a class called system. This class cannot be used by itself, it must be extended by another class. The methods available in the system class will be created dynamically based on the name of the class which extends it. So if I have a database table called users, and I define a class called users which extends the system class, I will have crud methods available to me. So just with the following code:

Code: Select all

 
class user extends system {
 
}
 
I could then do:

Code: Select all

 
$user = new user();
$user->setName('foo');
$user->setPassword('password');
$user->save();
 
Now, I want the passwords to be hashed, so I can simply modify the user class:

Code: Select all

 
class user extends system {
    protected function setPassword($pass) {
        return parent::setPassword(md5($pass));
    }
}
 
Make sense?
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: Polymorphic Extendable Base Class

Post by allspiritseve »

Yep, definitely sounds like ActiveRecord. You can get the current class name with get_class($this), and then it's just a matter of constructing the query.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Polymorphic Extendable Base Class

Post by Benjamin »

Ok cool. What's the best open source active record class? I would like one that supports foreign keys and can be used with a criteria class similar to symfony.
Paul Arnold
Forum Contributor
Posts: 141
Joined: Fri Jun 13, 2008 10:09 am
Location: Newcastle Upon Tyne

Re: Polymorphic Extendable Base Class

Post by Paul Arnold »

astions wrote:Ok cool. What's the best open source active record class? I would like one that supports foreign keys and can be used with a criteria class similar to symfony.
CodeIgniter comes with an activerecord class. Have a look at that.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Polymorphic Extendable Base Class

Post by josh »

astions wrote:

Code: Select all

 
class user extends system {
    protected function setPassword($pass) {
        return parent::setPassword(md5($pass));
    }
}
 
Make sense?
Yeah but for that I'd create a template method you can override on the system class, so you don't have that static call
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Polymorphic Extendable Base Class

Post by Benjamin »

That's how you call methods in a parent class from a class that has the same method name. It's not really static. AFAIK. Am I missing something?
Minty
Forum Newbie
Posts: 6
Joined: Sat Jun 27, 2009 7:46 pm

Re: Polymorphic Extendable Base Class

Post by Minty »

I have made something like this following the active record pattern to a degree, similar to frameworks such as CakePHP and CodeIgniter (and of course, Rails). Personally I've called it a 'Model' class it works like this:

Code: Select all

 
<?php
 
include_once 'classes/model.php';
 
Class User extends Model {
 
}
 
$model = Model::getModel('User');
$model->findAllByName($name, array('orderBy' => 'field asc'));
$model->findFirstById($id);
$model->save([array or object of data]);
 
?>
 
Features such as relations and validation are coded into the User object such as:

Code: Select all

 
<?php
 
Class User extends Model {
 
   protected $validate = array(
      'fieldName' => array([validation options]),
      'name' => array('maxlength' => 32, 'required', 'unique')
   );
   
   protected $hasMany = array(
      'posts' => array(
         'modelName' => 'Post',
         'foreignKey' => 'user_id',
      )
   );
 
   /* Above could also be simply be written as :
   protected $hasMany = array('posts');*/
 
}
 
Methods are naturally extensible, i.e.

Code: Select all

 
Class Image extends Model {
 
   protected function _find($params) {
      $result = parent::_find($params);
      $this->getImageFile($result);
   }
 
}
 
I assume this is the sort of thing you're talking about?

My class does not generate helper get/set methods as your proposition entails, however doing so would be relatively easy using the __call magic function (although it already looks rather bloated with the 'find' functionality).
BornForCode
Forum Contributor
Posts: 147
Joined: Mon Feb 11, 2008 1:56 am

Re: Polymorphic Extendable Base Class

Post by BornForCode »

I think is more like TableGateway.

You implement in that base class, usually an abstract class general behavior and in the extended class you have all operations available. This can be tricky when you want to create cascade operations. PK from [table 1] is referred in [table 2] and the pk of [table 2] is referred in [table 3] and you decide that you want to delete something from [table 1]
Post Reply