Re: Using a Resource as a Class Property
- From: Jerry Stuckle <jstucklex@xxxxxxxxxxxxx>
- Date: Sun, 22 Jul 2007 22:28:28 -0400
Michael Fesser wrote:
.oO(Sanders Kaufman)
Michael Fesser wrote:Correct. PHP doesn't do that automatically, except if the child classGot it - and I wish I'd got it a year ago!
doesn't have a constructor. Then PHP will call the one from the base
class, if it exists. But as soon as your child class uses its own
constructor, you have to manually call the parent one. Usually this
should be the first action.
Is that a normal OOP way to do it - done that way in C++ and whatnot - or is this unique to PHP?
It works like that in all languages I know and it simply _has_ to work
that way. The programmer has to be able to exactly define if and when a
parent method will be called. Just consider this little example:
class A {
protected $data = NULL;
public function __construct() {
$this->doSomething();
}
private function doSomething() {
// do something with $this->data
}
}
class B extends A {
public function __construct() {
$this->data = 'foo';
parent::__construct();
}
}
Quite simple: A base class with a member variable and a method that
performs some action with that. Since this is done in the constructor,
every child class has to be able to initialize the data before calling
the parent constructor, as you can see in B::__construct(). If PHP would
automatically call the parent constructor, this would be impossible.
Micha
Micha,
Generally unique to PHP. Java, C++ and SmallTalk all call the base class constructor automatically before any code is executed in the child class constructor.
Also, the parent should never have a dependency on the child class, and the parent's class constructor should never depend on anything in the child class other than what is passed to the constructor.
Your case is improper OO. What if an instance of A is created instead of B? It wouldn't work.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@xxxxxxxxxxxxx
==================
.
- Follow-Ups:
- 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
- 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
- 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
- Re: Using a Resource as a Class Property
- From: Michael Fesser
- Using a Resource as a Class Property
- Prev by Date: Re: Using a Resource as a Class Property
- Next by Date: Re: Problem with PHP output
- 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
|