Re: help with inheritance problem -- again
| From: | David P. Schwartz | Date: | Tue, 14 Nov 2000 09:00:44 +0000 |
| Subject: | Re: help with inheritance problem -- again | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-25158@lists.php.net to get a copy of this message | ||
Arrrrgggghhh!!! It's really tough trying to post something as non-trivial as this
without just posting all the code, and it's rather messy at the moment. (I'm in
the process of converting from non-classes to classes, and you really don't want to
see it!) You pointed out more transcription errors...
Thanks for giving it a look, though. See below for a question
Brian Clark wrote:
> David P. Schwartz wrote:
> <snip>
> >
> >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().
another typo. sorry. This _is_ supposed to be the constructor,
base_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?
>
> base_table() in your example isn't a method, it's the name of a class -
> that's all, unless you rename db_table().
The problem is, PHP3 doesn't support double-colon syntax, like c++, so a reference
like base_table::base_table($db_obj) isn't legal. The three PHP books I have all
cover PHP4 and just have trivial constructors that aren't very explanatory about
initializing base classes. They just say that if you use a ctor in a derived class
then you must call the base class ctor explicitly. But how is this done in PHP3?
> 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.
>
This code is contrived. I'm creating an instance of the my_db object, which
initializes a db connection, and then placing a copy inside of some (base) table
objects so they can access the same MySQL connection value. None of the methods in
the derived table objects can seem to see it, nor can anything outside of them.
Considering that PHP doesn't offer any privacy to data members, I figure I'm not
referencing it properly.
What I want is to know how to access the $connection property inside an object
(my_db) that itself is a property of the base class (base_table) from an instance
of the derived class (derived_tbl). That is, given a $d_tbl object, how do I get
access to the db->$connection property contained inside of it? In c++ it would be
analogous to d_tbl->db->connection, or in object pascal it would be
d_tbl.db.connection (both assuming public visibility).
>
> Maybe I haven't been keeping up with the docs, but what in Sam Hell is
> emit()? :-)
it's a word that's a letter shorter than 'print' and has a few extra things
thrown
in for debugging :-)
> Did you mean print?
uh, yea, that's the ticket...
> >//=====================
> >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
Thanks
-David