473,228 Members | 1,830 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,228 software developers and data experts.

Inheritance of static member variables


I've a class that defines a number of DB operations, and I need to do
something very similar but operating on a different table.

I had wanted to do something like the following:


abstract class BaseClass{
const table_name;

public static function getFoo(){
$sql = "GET foo FROM ".self::table_name." WHERE 1";
// return a result

class SubClass{
const table_name='bar';


However, this doesn't work. I see the reasons have been talked about a
lot on this group so I won't go into that discussion again!

I realise that I could instance the class and make the method
non-static, but the problem is that the existing class is already
widely used in a static context so I can't change that at this stage
unless I somehow provide a static call too.

My question is, how can I do something similar? How can I keep all the
shared functionality in the base class and just define a few env.
variables in the subclasses without having to repeat my methods?



Aug 15 '05 #1
2 3318
Some people think this is a bug in PHP and other's don't. Check out
the following:


I guess it has to do with the way that self:: is bound (apparently, at
runtime, self is bound to the class in which is it defined rather than
the inheriting class).

The most obvious solution would be to define some abstract static
method in BaseClass called getTableName() or something, then define it
in each inheriting (child) class. Unfortunately, you tend to run into
the same problem.

Although it may be undersirable, another alternative would be to define
the constant table_name in the child class, then have getFoo() take the
table name as a parameter inside BaseClass. Although you'd then have
to override getFoo() in each child, it would be simple:

public static function getFoo() {
return parent::getFoo(self::table_name);

For safety sake, you can also declare getFoo() as protected inside
BaseClass. Again, not the most desirable solution (I'd prefer to see
the "bug" fixed) but I have yet to see a better way.

Anyone else have any thoughts?

Aug 16 '05 #2
Hm, thanks for that Zeldorblat.

One other slight wrinkle is that I have more than one constant in the
class that needs accessing (I was just simplifying for clarity), so
it'd be better if I could pass the entire class rather than the const
value. Is there any way of doing that? Trying to pass 'self' isn't
particularly successful, I've resorted to:


abstract class BaseClass{
public static function getFoo($class){
$sql = "GET foo FROM ".$class->table_name." WHERE 1";
// return a result

class SubClass extends BaseClass{
public $table_name='bar';
public static function getFoo(){
return parent::getFoo(new self());


But having to instance the class seems quite messy.

It's a real shame there isn't another token that works the way a lot of
people seem to expect 'self' to work! Maybe no_really_I_mean_self:: ?


Aug 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: bingomanatee | last post by:
I am having a problem with inheritance and static properties. I am writing a data record class object structure for Xoops. Because the table name is dynamic, I have to set it as a static member...
by: Asapi | last post by:
When a derived class inherits from a base class, does the former inherits everything, including public/protected/private instance data, static data, and various methods(static, private/public,...
by: cppaddict | last post by:
Hi, Say I have a abstract base class and I know that every derived class will have a static data member (call it SharedInformation) representing information to be shared across all instances of...
by: Taylor | last post by:
I'm trying to understand inheritance. I'd like to make my own type of IPAddress lets call it myIp. The following gives me CS0029 error: Cannot implicitly convert type 'System.Net.IPAddress' to...
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
by: mike | last post by:
Ok, If I have the following code: //main.cpp class Base { public: int static a;
by: mangalalei | last post by:
A static data member can be of the same class type as that of which it is a member. A nonstatic data member is restricted to being declared as a pointer or a reference to an object of its class. ...
by: Chukkalove | last post by:
Hi I have an abstract class that contains 100% static methods and variables. One of the member variables "string DatabaseName" needs to be overridden in derived classes. Am I able to keep my...
by: crjjrc | last post by:
Hi, I've got a base class and some derived classes that look something like this: class Base { public: int getType() { return type; } private: static const int type = 0; };
by: BillE | last post by:
I have a C# console application. The class which contains the "static void Main()" method inherits from a base class. The base class contains a function GetSomething() When I try to call...
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.