|
Posted by Michael Fesser on 07/22/07 15:57
..oO(Sanders Kaufman)
>OK - that was weird, and took some debugging.
>I knew there was *something* about scope to deal with here.
>
>I'm aggragating the database object into a base class, and then creating
>other classes that "extend" that base class.
>
>HOWEVER, in order to do that, I had to move the instantiation(?) of the
>database class into the extended class.
This shouldn't be necessary, as long as you're correctly calling the
inherited methods from your derived class when necessary. Additionally
in PHP 4 there are no visibility options as in PHP 5 (public, protected,
private), so all member variables and methods are always public and
accessible from everywhere.
>When I had it in the base
>class, the class that extended it could not see the database properties
>and functions.
Hmm. Let's take your first example again (shortened):
class my_baseclass {
var $Database;
function my_baseclass() {
$this->Database = new my_database();
...
}
}
Now let's extend it:
class my_childclass extends my_baseclass {
}
This class will pretty much do the same as the base class, since no
methods are overwritten. But if you want to overwrite some methods to
extend their functionality, you should always also call the inherited
method. Let's assume you overwrite the constructor:
class my_childclass extends my_baseclass {
function my_childclass() {
// first call parent constructor or we won't have a database ...
parent::my_baseclass();
// then do some other stuff
...
}
}
Micha
Navigation:
[Reply to this message]
|