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

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($pID){
$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(sprintf("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 1559
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.class.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

"bingomanatee" <ed******@manateebay.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.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($pID){
$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(sprintf("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*******@attglobal.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_conn;
...
$sql = sprintf(self::SELECT_SQL, $this->get_table_name(),
sprintf(self::WHERE_ID_SQL, $this->ID));
if (!$result) return Feedback::db_error($this, 'get_array',
"(object $this->obj_ID) Database error:");
$array =&
self::strip_slash_array($Data_class_conn->fetchArray($result));
$Data_class_conn->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_table_name; }

} // end Dir_class

Dir_class::$x_table_name = XOOPS_DB_PREFIX . '_' .
Dir_class::TABLE_NAME;

Jul 29 '05 #5

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

Similar topics

5
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. ...
14
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...
29
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
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...
36
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...
2
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: ...
8
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...
12
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...
3
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...
3
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:...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...

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.