Re: Using a Resource as a Class Property



..oO(Sanders Kaufman)

Michael Fesser wrote:

Correct. PHP doesn't do that automatically, except if the child class
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.

Got it - and I wish I'd got it a year ago!

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
.



Relevant Pages

  • Re: Using a Resource as a Class Property
    ... But as soon as your child class uses its own ... constructor, you have to manually call the parent one. ... Is that a normal OOP way to do it - done that way in C++ and whatnot - or is this unique to PHP? ...
    (comp.lang.php)
  • Re: Using a Resource as a Class Property
    ... automatically call the parent constructor, ... class constructor automatically before any code is executed in the child class constructor. ... PHP doesn't automatically call constructors BUT doing so would be more in keeping with the Tao of OOP. ... I seriously considered writing the classes in a way to where the parent would *presume* all kinds of stuff about the child, but it didn't jibe with my obsession with atomicity - so I didn't do it. ...
    (comp.lang.php)
  • OOP, constructor and visibility
    ... My OOP text states that a private property can be made visible in the ... constructor, without a "set" method, but I can't make it work. ... public function __construct{ ... Or does this have something to do with the configuration of my PHP? ...
    (comp.lang.php)
  • Re: finding compile time errors
    ... class FormWidgets{ ... public function makePopupMenu{ ... -- Constructor will always return an instance of the class no matter what you return ... Can I just name a file with the pdo extension and will php use it without have to call include or require? ...
    (comp.lang.php)
  • Re: finding compile time errors
    ... class FormWidgets{ ... public function makePopupMenu{ ... I'm trying to learn the php way. ... I see now that my constructor is useless. ...
    (comp.lang.php)