470,848 Members | 1,054 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,848 developers. It's quick & easy.

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 :)
Jun 27 '08 #1
7 1201
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.

Jun 27 '08 #2
On Mon, 16 Jun 2008 07:23:59 +0200, Daniel Smedegaard Buus
<da********@gmail.comwrote:
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
Jun 27 '08 #3
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.
js*******@attglobal.net
==================

Jun 27 '08 #4
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
Jun 27 '08 #5
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.
js*******@attglobal.net
==================

Jun 27 '08 #6
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
Jun 27 '08 #7
On 16 Jun., 18:17, Alexey Kulentsov <a...@inbox.ruwrote:
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! :)
Jun 27 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Jeff Thies | last post: by
6 posts views Thread by Eddie | last post: by
4 posts views Thread by Studio P.M. | last post: by
3 posts views Thread by Arne Beruldsen | last post: by
4 posts views Thread by Pere Raphael | last post: by
4 posts views Thread by Venkatraman.S. | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.