help with inheritance problem -- again
| From: | David P. Schwartz | Date: | Tue, 14 Nov 2000 05:08:07 +0000 |
| Subject: | help with inheritance problem -- again | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-25142@lists.php.net to get a copy of this message | ||
I'm reposting this due to a critical typo in the original... Sorry.
=======================
Hi.
I'm working with PHP3 and am trying to create some classes. Something isn't
working as expected while attempting to access variables in base classes.
class my_db
{
var $host_name, $user_name, $user_pwd, $db_name, $connection;
function my_db()
{
$this->db_connect();
}
function db_connect() # this works ok
{
$this->ts = time();
$this->host_name = ...
$this->user_name = ...
$this->user_pwd = ...
$this->db_name = ...
$this->connection = @mysql_connect( $this->host_name,
$this->user_name,
$this->user_pwd )
or die( "Couldn't connect to host" );
$db = @mysql_select_db( $this->db_name, $this->connection )
or die("Couldn't select database" );
return $this->connection;
}
} # my_db
class base_table
{
var $db;
// constructor
// pass this an instance of a mydb object
function db_table( $db_obj )
{
$db = $db_obj;
}
function so_something_with_base()
{
// here, I want to access $this->db->connection in base class,
// but I cannot seem to get it.
$bdb2 = $this->db;
$conn = $bdb2->connection; # <<--------------- line 116
. . .
}
. . .
} # base_tbl
class derived_tbl extends base_table
{
// constructor
// pass this an instance of an mydb object
function derived_tbl( $db_obj )
{
$this->base_table( $db_obj ); # is this proper syntax?
# It seems to work...
}
}
//=====================
called in PHP3 script like so:
. . . .
$mydb = new my_db(); # this works ok
$d_tbl = new derived_tbl( $mydb ); # this _seems_ to work, but...
$db2 = $d_tbl->db; # scalar? Should be db object in base_tbl
emit("db2->connection = [$db2->connection]<br>"); # <<-------
line 55
//=====================
Here's what I get trying to run the above script:
Warning: Using scalar variable $db2 as an array or object in ..script file.. on
line 55
Warning: Object bdb2 not found in classes.php on line 116
All the books I have describe PHP4, with a few caveats for PHP3. Can anybody
give me an idea of what's wrong here?
Thanks
-David