473,545 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_nam es))
FROM
self::$table_na me
";
// ...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 1314
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_nam es))
FROM
self::$table_na me
";
// ...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********@gma il.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_nam es))
FROM
self::$table_na me
";
// ...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_nam es))
FROM
self::$table_na me
";
// ...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*******@attgl obal.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*******@attgl obal.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.ruw rote:
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
5949
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 contentWindow.document or the contentDocument depending on whether this was IE or Mozilla. (Thanks Lasse.) I'm not finding this.
6
20491
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: text-decoration : underline overline; when reading element.currentStyle.textDecoration I only get "underline"!! What's worse is that...
4
1374
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 properties like image, collections such as tabpage's cannot done with this method. Anybody can help me on how to save and later restore the complex...
4
17667
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 I’ve found a way to get the parameters 1) and 2), but not 3). Here and there, though, parameter 3) is mentioned as a legitimate element of the Active...
4
1624
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 e.Item.FindControl("name-of-my-DropDownList"), where e is a DataGridCommandEventArgs passed to the UpdateCommand of the DataGrid, I get an empty DropDownList. In fact,...
3
2165
by: Arne Beruldsen | last post by:
How do you read the serial number to a hard drive? Thanks...Arne
4
3663
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 use ADSIEDIT, I can see the value in the format 0x077 0x043 0x064 0x055 0x072 0x054 0x44 0x058 which I can convert by using Character Map. Does...
0
8145
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 I'm not able read the property defined under this section. those are like this User Settings -Testing Application Settings -Test
2
5015
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 OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + csvPath + ";Extended Properties=\"Text;HDR=Yes;FMT=Delimited\"");
4
1077
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 in Python) wherein the properties change frequently and the program needs to work according to the new rules. Since , python is scripting language...
0
7502
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main...
0
7946
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7457
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3491
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1921
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1045
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
744
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.