473,659 Members | 2,636 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inheritance problem with PHP 5 objects

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
for each class. However I am having trouble finding a way to pass this
static member to the subclass.

for instance,

/************ SAMPLE RECORD CLASS ***************/

class Address_class extends Data_class {
const TABLE_NAME = 'address';
public static $x_table_name = '';
public ID = 0;

public function __construct($pI D){
$this->ID = $pID;
$this->get();
}
}

Address_class:: $x_table_name = OPS_DB_PREFIX . '_' .
Address_class:: TABLE_NAME;

/************** CORE DATA CLASS ***************/

abstract class Data_class {

function get(){
$result = $this->execute_SQL(sp rintf("SELECT * FROM %s WHERE ID =
%d LIMIT 1;", self::$x_table_ name, $this->ID));
{--- process result --}
}
}

/*************** *** END CODE *************** *****/

The problem is that becuase it is static and in the child class, the
value of self::$x_table_ name is undefined in the context of the parent
method get(); where because it is a normal property, I can pass ID just
fine.

Any thoughts

Jul 21 '05 #1
4 1572
Your approach is all wrong. You are defining the table name as a constant
and not putting anything in the static variable. You ought to try what I
have been doing for the past couple of years, which is to set the table name
within the constructor of the subclass, like this....

require_once 'std.table.clas s.inc';
class Person extends Default_Table {
function Person()
{
$this->tablename = 'person';
$this->dbname = 'sample';

} // Person
} // end class

Note that the variables $tablename and $dbname are defined in the superclass
and do not need to be redefined in each subclass.

Hope this helps.

--
Tony Marston

http://www.tonymarston.net

"bingomanat ee" <ed******@manat eebay.com> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.com.. .
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
for each class. However I am having trouble finding a way to pass this
static member to the subclass.

for instance,

/************ SAMPLE RECORD CLASS ***************/

class Address_class extends Data_class {
const TABLE_NAME = 'address';
public static $x_table_name = '';
public ID = 0;

public function __construct($pI D){
$this->ID = $pID;
$this->get();
}
}

Address_class:: $x_table_name = OPS_DB_PREFIX . '_' .
Address_class:: TABLE_NAME;

/************** CORE DATA CLASS ***************/

abstract class Data_class {

function get(){
$result = $this->execute_SQL(sp rintf("SELECT * FROM %s WHERE ID =
%d LIMIT 1;", self::$x_table_ name, $this->ID));
{--- process result --}
}
}

/*************** *** END CODE *************** *****/

The problem is that becuase it is static and in the child class, the
value of self::$x_table_ name is undefined in the context of the parent
method get(); where because it is a normal property, I can pass ID just
fine.

Any thoughts

Jul 22 '05 #2
That is of course a good and functional solution. I might be just
pushing things too far, but ideally, as the table name is constant for
every record, I'd like the efficiency of being able to store it once,
statically.

And beyond that, I'd like to know the answer to the principle behind
the question:

IS IT POSSIBLE for a parent method to access the static fields of a
child class?

Jul 22 '05 #3
bingomanatee wrote:
That is of course a good and functional solution. I might be just
pushing things too far, but ideally, as the table name is constant for
every record, I'd like the efficiency of being able to store it once,
statically.

And beyond that, I'd like to know the answer to the principle behind
the question:

IS IT POSSIBLE for a parent method to access the static fields of a
child class?


Not reliably.

The child will always have and know about its parent. The parent never knows
about the child and may not have one.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Jul 23 '05 #4
aha - I got it!

FYI this class uses the XOOPS libraries to handle data management, so
the XOOPS_DB_PREFIX is a runtime constant. (which is why the table name
is stored in a static, not a const.)

Using this methodology, you don't keep a duplicate of the table name
with each instance of the record, and most of the data handling
mechanisms can be buried in the base Data_class class.

/******** CODE BEGINS *************** */

abstract class Data_class {

abstract function get_table_name( );

protected function &get_array()
{
global $Data_class_con n;
...
$sql = sprintf(self::S ELECT_SQL, $this->get_table_name (),
sprintf(self::W HERE_ID_SQL, $this->ID));
if (!$result) return Feedback::db_er ror($this, 'get_array',
"(object $this->obj_ID) Database error:");
$array =&
self::strip_sla sh_array($Data_ class_conn->fetchArray($re sult));
$Data_class_con n->freeRecordSet( $result);
return $array;
}
} // end Data_class

class Dir_class extends Data_class
{
public static $x_table_name = '';

/*********** TABLE NAME ACCESS
*************** *************** *******/

public function get_table_name( ){ return Dir_class::$x_t able_name; }

} // end Dir_class

Dir_class::$x_t able_name = XOOPS_DB_PREFIX . '_' .
Dir_class::TABL E_NAME;

Jul 29 '05 #5

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

Similar topics

5
1661
by: Robert Spoons | last post by:
Can you look over this code, preferably try it, and comment? I believe the 'extend' function below will allow you to use full 'class inheritance' in javascript, but I would like to verify it. The extend function allows the following: 1) inheriting from multiple classes, 2) inheriting an inherited classes inheritances (awkward to say), 3) inheriting appended prototype methods and properties of inhertited classes.
14
12901
by: Steve Jorgensen | last post by:
Recently, I tried and did a poor job explaining an idea I've had for handling a particular case of implementation inheritance that would be easy and obvious in a fully OOP language, but is not at all obvious in VBA which lacks inheritance. I'm trying the explanation again now. I often find cases where a limited form of inheritance would eliminate duplication my code that seems impossible to eliminate otherwise. I'm getting very...
29
4651
by: MAHESH MANDHARE | last post by:
Hi , Can Anyone explain me exactly why multiple inheritance not used in java and c# thanks, Mahesh -- Have A Good Day, Mahesh, Maheshmandhare@yahoo.co.in
31
1927
by: John W. Kennedy | last post by:
I quite understand about prototypes and not having classes as such, but I happen to have a problem involving blatant is-a relationships, such that inheritance is the bloody obvious way to go. I can think of several ad-hoc ways to achieve inheritance, but is there an accepted standard idiom? -- John W. Kennedy "But now is a new thing which is very old-- that the rich make themselves richer and not poorer, which is the true Gospel, for...
36
2698
by: Pacific Fox | last post by:
Hi all, haven't posted to this group before, but got an issue I can't work out... and hoping to get some help here ;-) I've got a base object that works fine with named arguments when called on it's own. However when I call the child object I get an error " has no properties (in firefox)." I simple test:
2
2087
by: beseecher | last post by:
Hi, In my research in the javascript language I have encountered problems with implementing prototype inheritance while preserving private methods functioning properly. Here is an example: function A(){ var _this = this; this.a = 10;
8
328
by: RSH | last post by:
Hi, I am working on some general OOP constructs and I was wondering if I could get some guidance. I have an instance where I have a Base Abstract Class, and 4 Derived classes. I now need to make a list class that will store the objects. My question is how do I go about creating the list class...I am assuming it should be a standalone class that uses an arraylist to store the objects. If I go that route how do I instantiate the...
12
2360
by: Massimo | last post by:
Hi to all, I'm facing a problem in a particularly complex inheritance hierarchy, and I'd like to know what the standard says about it and if my compiler is correct in what it does. I have two consecutive layers of diamond-shaped inheritance; the first layer is declared as using virtual inheritance, while the second one is declared as *not* using it. This is what I'd like to have:
3
1581
by: Jam | last post by:
Hello All, Your comment is needed on following subject,i define the Class and Inheritance as following,what do you think? Class: Class is a set of objects which shares common states and behaviors,Class can contain subclasses. Inheritance: if two classes are having two common states and behaviors or if a class is derived from another class it is called Inheritance, All's comment is needed,is this defination ok or something more should
3
2896
by: Leo Seccia | last post by:
Hello everyone, I have a c# project with a sql server database. I have a number of lookup tables in my database which I successfully managed to import into my LINQ dataclasses. eg. Table: tlkpColor (PK) tlkpColorID
0
8341
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8751
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8630
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7360
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4176
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2759
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
2
1982
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1739
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.