Re: Using a Resource as a Class Property



Sanders Kaufman wrote:
Jerry Stuckle wrote:
Michael Fesser wrote:

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.

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.


OK - to update my understanding...
PHP doesn't automatically call constructors BUT doing so would be more in keeping with the Tao of OOP.

Zat right?


That is true. And perhaps one day PHP will more closely follow the OO way of doing things. They've been getting closer with each release.


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.

Wow. Cool. When I built my classes, I didn't KNOW it should be done that way, but it FELT right.

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.

The child class defines the derivation, and therefore knows it has a parent class (and some things about that parent). However, the parent doesn't know if (or when) it is being used as a base class, so it should never assume things which may or may not be there (or set).

--
==================
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: PHP Limitation: Static Inheritance and Scope
    ... > child class, the method uses the parent class for scope (not the ... > Does anyone know if the PHP dev team has plans to remedy this issue? ... only scope that Parent::getNamehas is the static members of Parent. ...
    (comp.lang.php)
  • Re: PHP Limitation: Static Inheritance and Scope
    ... > child class, the method uses the parent class for scope (not the ... > Does anyone know if the PHP dev team has plans to remedy this issue? ... only scope that Parent::getNamehas is the static members of Parent. ...
    (comp.lang.php)
  • Re: adding existing parent object to extended child object?
    ... >>simply not possible to add another parent to that child class. ... >>to all OO languages, not just PHP. ... > Suppose the parent class is humans and the child class is personnel. ...
    (comp.lang.php)