|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Hello there
![]() I'm trying to implement some basic DRY MVC-like functionality as a basis to work on. Specifically, I'd like to create a base abstract model class with basic read, write, delete and update database functionality, that I can use for all models in my code. I'd pretty much like it to go like this (if there are a few errors, you'll have to forgive me, I just woke up and I don't have the code on me, getting ready to go to work, coffee not yet consumed :abstract class CmsModel { protected static function getAll() { $query = " SELECT $(implode(', ', self::$attr_names)) FROM self::$table_name "; // ...etc... } // ...etc... } class RssFeed extends CmsModel { protected static $table_name = "RssFeed"; protected static $attr_names = array("id", "name", "url"); } As you can probably tell, I'd like to be able to do a simple: $rss_feeds = RssFeed::getAll(); in my code. But for this to work, CmsModel must be able to read the two static properties that are only set in its derived classes, in this example in RssFeed. Is this possible? Everything works right now except this. I can overridet getAll() in the children and simply call the parent function with the right parameters, but this still means recreating the functions in all subclass definitions. I'd like to skip that ![]() Thanks in advance for any tips, Daniel ![]() |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
Daniel Smedegaard Buus schrieb:
> Hello there ![]() > > I'm trying to implement some basic DRY MVC-like functionality as a > basis to work on. Specifically, I'd like to create a base abstract > model class with basic read, write, delete and update database > functionality, that I can use for all models in my code. I'd pretty > much like it to go like this (if there are a few errors, you'll have > to forgive me, I just woke up and I don't have the code on me, getting > ready to go to work, coffee not yet consumed :> > abstract class CmsModel > { > protected static function getAll() > { > $query = " > SELECT > $(implode(', ', self::$attr_names)) > FROM > self::$table_name > "; > // ...etc... > } > // ...etc... > } > > class RssFeed extends CmsModel > { > protected static $table_name = "RssFeed"; > protected static $attr_names = array("id", "name", "url"); > } > > As you can probably tell, I'd like to be able to do a simple: > > $rss_feeds = RssFeed::getAll(); > > in my code. But for this to work, CmsModel must be able to read the > two static properties that are only set in its derived classes, in > this example in RssFeed. > > Is this possible? Everything works right now except this. I can > overridet getAll() in the children and simply call the parent function > with the right parameters, but this still means recreating the > functions in all subclass definitions. I'd like to skip that ![]() > > Thanks in advance for any tips, > > Daniel ![]() All in all, it is *not* OOP what you want to do. Completly redesign what you really want to do. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
On Mon, 16 Jun 2008 07:23:59 +0200, Daniel Smedegaard Buus
<danielbuus@gmail.com> wrote: > Hello there ![]() > > I'm trying to implement some basic DRY MVC-like functionality as a > basis to work on. Specifically, I'd like to create a base abstract > model class with basic read, write, delete and update database > functionality, that I can use for all models in my code. I'd pretty > much like it to go like this (if there are a few errors, you'll have > to forgive me, I just woke up and I don't have the code on me, getting > ready to go to work, coffee not yet consumed :> > abstract class CmsModel > { > protected static function getAll() > { > $query = " > SELECT > $(implode(', ', self::$attr_names)) > FROM > self::$table_name > "; > // ...etc... > } > // ...etc... > } > > class RssFeed extends CmsModel > { > protected static $table_name = "RssFeed"; > protected static $attr_names = array("id", "name", "url"); > } > > As you can probably tell, I'd like to be able to do a simple: > > $rss_feeds = RssFeed::getAll(); > > in my code. But for this to work, CmsModel must be able to read the > two static properties that are only set in its derived classes, in > this example in RssFeed. > > Is this possible? Everything works right now except this. Nope, not untill the static::$var functionality in PHP v5.3 > I can > overridet getAll() in the children and simply call the parent function > with the right parameters, but this still means recreating the > functions in all subclass definitions. I'd like to skip that ![]() http://en.wikipedia.org/wiki/Factory_method_pattern -- Rik Wasmus ....spamrun finished |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
Daniel Smedegaard Buus wrote:
> Hello there ![]() > > I'm trying to implement some basic DRY MVC-like functionality as a > basis to work on. Specifically, I'd like to create a base abstract > model class with basic read, write, delete and update database > functionality, that I can use for all models in my code. I'd pretty > much like it to go like this (if there are a few errors, you'll have > to forgive me, I just woke up and I don't have the code on me, getting > ready to go to work, coffee not yet consumed :> > abstract class CmsModel > { > protected static function getAll() > { > $query = " > SELECT > $(implode(', ', self::$attr_names)) > FROM > self::$table_name > "; > // ...etc... > } > // ...etc... > } > > class RssFeed extends CmsModel > { > protected static $table_name = "RssFeed"; > protected static $attr_names = array("id", "name", "url"); > } > > As you can probably tell, I'd like to be able to do a simple: > > $rss_feeds = RssFeed::getAll(); > > in my code. But for this to work, CmsModel must be able to read the > two static properties that are only set in its derived classes, in > this example in RssFeed. > > Is this possible? Everything works right now except this. I can > overridet getAll() in the children and simply call the parent function > with the right parameters, but this still means recreating the > functions in all subclass definitions. I'd like to skip that ![]() > > Thanks in advance for any tips, > > Daniel ![]() > But that would be the correct way to do it. A base class knows nothing about any derived class - in fact, the derived class may not even exist. However, if you get over the protected (which should be used seldom, if at all) and use get functions in the child class (i.e. getTableName(), getAttributes(), etc.), it works fine. -- ================== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp. jstucklex@attglobal.net ================== |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
Thanks all, I decided to skip the static stuff, and just go with
instantiating everything. It works great now Now, my next issue isto find out how to add methods to an object at runtime... Hope that's possible. I really miss a lot of the stuff in Ruby ![]() Cheers, Daniel |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
Daniel Smedegaard Buus wrote:
> Thanks all, I decided to skip the static stuff, and just go with > instantiating everything. It works great now Now, my next issue is> to find out how to add methods to an object at runtime... Hope that's > possible. I really miss a lot of the stuff in Ruby ![]() > > Cheers, > Daniel > Not really possible, but again, that's not good OO practices. It makes code much harder to manage. But then you're not doing Ruby now. You need to be thinking how do it in more conventional OO methods like PHP uses. Don't get me wrong - I think Ruby is a fine language. But it allows some things which can be easy to code - but hard to troubleshoot at a later time. -- ================== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp. jstucklex@attglobal.net ================== |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
Daniel Smedegaard Buus write:
> Thanks all, I decided to skip the static stuff, and just go with > instantiating everything. It works great now Now, my next issue is> to find out how to add methods to an object at runtime... Hope that's > possible. I really miss a lot of the stuff in Ruby ![]() http://www.php.net/__call http://www.php.net/manual/en/functio...method-add.php |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
On 16 Jun., 18:17, Alexey Kulentsov <a...@inbox.ru> wrote:
> Daniel Smedegaard Buus write: > > > Thanks all, I decided to skip the static stuff, and just go with > > instantiating everything. It works great now Now, my next issue is> > to find out how to add methods to an object at runtime... Hope that's > > possible. I really miss a lot of the stuff in Ruby ![]() > > http://www.php.net/__callhttp://www....method-add.php Thank you, Alexey, exactly what I wanted! ![]() |
|
![]() |
| Outils de la discussion | |
|
|