Re: Using a Resource as a Class Property
- From: Michael Fesser <netizen@xxxxxx>
- Date: Sun, 22 Jul 2007 17:57:37 +0200
..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
.
- Follow-Ups:
- Re: Using a Resource as a Class Property
- From: Sanders Kaufman
- Re: Using a Resource as a Class Property
- References:
- Using a Resource as a Class Property
- From: Sanders Kaufman
- Re: Using a Resource as a Class Property
- From: Michael Fesser
- Re: Using a Resource as a Class Property
- From: Sanders Kaufman
- Re: Using a Resource as a Class Property
- From: Michael Fesser
- Re: Using a Resource as a Class Property
- From: Sanders Kaufman
- Using a Resource as a Class Property
- Prev by Date: Re: Cannot modify header information - headers
- Next by Date: Re: Are PHP libraries linked dynamically or statically?
- Previous by thread: Re: Using a Resource as a Class Property
- Next by thread: Re: Using a Resource as a Class Property
- Index(es):
Relevant Pages
|