PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > Reading properties in non-instantiated child class from the parentclass
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Reading properties in non-instantiated child class from the parentclass

Réponse
 
LinkBack Outils de la discussion
Vieux 16/06/2008, 06h23   #1
Daniel Smedegaard Buus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Reading properties in non-instantiated child class from the parentclass

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
  Réponse avec citation
Vieux 16/06/2008, 13h29   #2
Olaf Schinkel
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Reading properties in non-instantiated child class from the parentclass

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.

  Réponse avec citation
Vieux 16/06/2008, 13h33   #3
Rik Wasmus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Reading properties in non-instantiated child class from the parent class

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
  Réponse avec citation
Vieux 16/06/2008, 13h41   #4
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Reading properties in non-instantiated child class from the parentclass

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
==================

  Réponse avec citation
Vieux 16/06/2008, 15h06   #5
Daniel Smedegaard Buus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Reading properties in non-instantiated child class from theparent class

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
  Réponse avec citation
Vieux 16/06/2008, 16h28   #6
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Reading properties in non-instantiated child class from theparent class

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
==================

  Réponse avec citation
Vieux 16/06/2008, 17h17   #7
Alexey Kulentsov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Reading properties in non-instantiated child class from theparent class

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
  Réponse avec citation
Vieux 17/06/2008, 08h32   #8
Daniel Smedegaard Buus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Reading properties in non-instantiated child class from theparent class

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!
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h56.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,17714 seconds with 16 queries