Re: Using a Resource as a Class Property



Michael Fesser wrote:
.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

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
==================
.



Relevant Pages

  • Re: What does the CLR do when you instantiate an object? - Continue
    ... > cast an object to one of its parent types then yes I'd agree with you. ... >child class in the memory, when I asked you whether you are confident to ... in the PARENT CONSTRUCTOR we have shown working examples ...
    (microsoft.public.dotnet.framework.clr)
  • Re: Sub class constructor
    ... If you want to use parent constructors when instanciating your child class, ... public sub New ... constructors not parent class constructor; ...
    (microsoft.public.dotnet.languages.vb)
  • Re: OOP: constructors of base class in new()
    ... constructor of the parentclass does this too, ... This has the advantage that if you rename a parent class, ... Ramprasad A Padmanabhan wrote: ... But is there a way I can tell php to execute all ...
    (comp.lang.php)
  • Re: What does the CLR do when you instantiate an object? - Continue
    ... process right at the beginning of the ChildClass constructor. ... built before the parent constructor is called. ... exists and it is created before the object of the child class instantiation ...
    (microsoft.public.dotnet.framework.clr)
  • 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)