473,396 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 1308
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jeff Thies | last post by:
I'm (still) having trouble reading the contents of an IFRAME (IE5). I have this: var my_iframe = document.frames; What I would want to do at that point is get either the...
6
by: Eddie | last post by:
When I use JavaScript to read an element's textDecoration style, I only get one value even if there are more than one in the sytle sheet. For example if the text-decoration is defined as:...
4
by: Özden Irmak | last post by:
Hello, I'm trying to save some properties of some windows controls into a XML File. Most of the properties work when I save them as String (TypeConverter.ConvertToString). But some complex...
4
by: Studio P.M. | last post by:
Assuming that I need to know programmatically (VS C#) an User's, or Domain's, Password Policy parameters: 1) MinPasswordLength 2) PasswordHistoryLength 3) PasswordAttribute - COMPLEX so far...
4
by: starwiz | last post by:
I'm trying to use the DataGrid's editing with a DropDownList. I've tried using every code sample I've seen and none of them seem to be able to solve my problem. When I call...
3
by: Arne Beruldsen | last post by:
How do you read the serial number to a hard drive? Thanks...Arne
4
by: Pere Raphael | last post by:
Hi all, I am writng some code to read AD user properties but I am not having much luck reading octet values. For example, there is a property userPassword which returns System.Byte. When I...
0
by: Lucky | last post by:
Hi guys, I've got a problem under reading settlings from App.config file. I've used System.Configuration to reach to the "User Settings" and "Application Settings" sections. the problem is now...
2
by: tshad | last post by:
I have a program that is reading a csv file into a dataset. I want it to read the 1st line as data. But it ignores it. I have the Connection set up as: OleDbConnection csvConnection = new...
4
by: Venkatraman.S. | last post by:
Hi, Am sure many would have stumbled on this situation while developing an application in Python which is highly driven by configuration/ properties. I have an application (obviously written...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.