Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Child class contructors and their parents 1

Status
Not open for further replies.

Dustman

Programmer
Joined
May 7, 2001
Messages
320
Location
US
Tell me if I'm correct on this..

I have a parent class called Email.. the constructor is

Code:
Email($to=null,$from=null,$subject=null,$body=null)

I have a child class EmailCar extends Email.. its constructor is

Code:
function EmailCar($to=null,$from=null,$subject=null,$body=null) {
    $this->Email($to,$from,$subject,$body);
}

This should work fine.

Q1. According to the documentation, if I don't have a constuctor in my child class, it will use the parents constuctor. Therefore, I don't need my EmailCar constructor unless I want to initialze other variables that only apply to the EmailCar class. Right?

Q2. If I understand the docs right, you can only have one "generation".. that is, many children but no grandkids. Meaning, I could never have class EmailCarRed extends EmailCar correct?

-Dustin
Rom 8:28
 
A1. That is correct.

In the following code:

Code:
<?php
class A {
   function A() {
     echo &quot;I am the constructor of A.<br />\n&quot;;
   }
}

class B extends A {
   function C() {
       echo &quot;I am a regular function.<br />\n&quot;;
   }
}

// no constructor is being called in PHP 3.
$b = new B;
?>

The output will be &quot;I am the constructor of A.&quot;

If your derived class has a constructor of its own and you want to invoke the contructor of the parent class, you must do so explicitly in your code.



A2. No, you can perform serial multiple extensions of classes.

The following code:

Code:
<?php
class A
{
   function A()
   {
     echo &quot;I am the constructor of A.<br />\n&quot;;
   }

   function Af()
   {
     echo &quot;I am a regular function in A.<br />\n&quot;;
   }
}

class B extends A
{
   function Bf()
   {
       echo &quot;I am a regular function in B.<br />\n&quot;;
   }
}

class C extends B
{
   function Cf ()
   {
   	echo &quot;I am a regular function in C.<br />\n&quot;;
   }
}

// no constructor is being called in PHP 3.
$c = new C;

print_r (get_class_methods($c));
?>

Will return something like:

Code:
I am the constructor of A.


Array
(
    [0] => a
    [1] => af
    [2] => bf
    [3] => b
    [4] => cf
    [5] => c
)


Want the best answers? Ask the best questions: TANSTAAFL!!
 
Thanks.. the docs seem a little confusing on that issue. I was begining to wonder why they would limit php like that.

I know php 4 is not fully OOP.. what are the main things to watch out for when dealing with classes and inheritance? How does it handle overwriting parent functions/variables and how does it handle overloading funtions?

-Dustin
Rom 8:28
 
PHP is about as OO as it needs to be.

Remember that multiple inheritance is not supported in PHP versions < 5.0. But serial inheritance is.

If function namess are reused between a child and a parent, the child's function takes precedence. You can also access the parent's functions through the use of the &quot;::&quot; namespace operator.

If you reuse variable names, the child's variables also take precedence. There is no way to access the parent's variables. But then there's no need for it, either.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Thats exactly what I was hoping for. Thanks for the input. Just for clearification cause I know somebody is going to ask, multiple inheritance is one child having many parents and serial is one child having only one parent. I guess you would call php a &quot;disfunctional family&quot;. Thats just a play on words though.. it is far from disfunctional. I'm looking forward to seeing all the new toys in php 5.

-Dustin
Rom 8:28
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top