Re: help with inheritance problem -- again

From: Date: Tue, 14 Nov 2000 08:20:30 +0000
Subject: Re: help with inheritance problem -- again
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-25155@lists.php.net to get a copy of this message
David P. Schwartz wrote:
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 )
For this to be a constructor, you'll need to name it base_table() not db_table().
    {
         $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?
base_table() in your example isn't a method, it's the name of a class - that's all, unless you rename db_table(). Fatal error: Call to unsupported or undefined function base_table() in /var/docs/test.php on line 60
                                          # 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
Above, $db2 is a scalar, not an object, so you can't access connection, which happens to be a property. What you want is $mydb->connection. Maybe I haven't been keeping up with the docs, but what in Sam Hell is emit()? :-) Did you mean print?
//===================== 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?
Making all the changes I mentioned above, and changing emit to print, gives me: mydb->connection = [1]<br> -Brian

« previous php.general (#25155) next »