Friday, January 11, 2013

Preserving the Functionality of the Parent [Final]

The following example makes use of both parent:: and self:: for accessing the Child and Ancestor classes:

saved as testInheritance.php
class Ancestor {

const NAME = "Ancestor";

function __construct()

{

print "In " . self::NAME . " constructor\n";

}

}

class Child extends Ancestor {

const NAME = "Child";

function __construct()

{

parent::__construct();

print "In " . self::NAME . " constructor\n";

}

}

$obj = new Child();


outputs:
In Ancestor constructor In Child constructor

No comments:

Post a Comment