Probably a silly question.. but how do I correctly use the methods of class Test in class Testb without overriding them?
<?php
class Test {
private $name;
public function __construct($name) {
$this->name = $name;
}
public function getName() {
return $this->name;
}
}
<?php
class Testb extends Test {
public function __construct() {
parent::__construct($name);
}
}
<?php
include('test.php');
include('testb.php');
$a = new Test('John');
$b = new Testb('Batman');
echo $b->getName();
You need to give the
Testbconstructor a$nameparameter too if you want to be able to initialize it with that argument. I modified yourTestbclass so that its constructor actually takes an argument. The way you currently have it, you should not be able to initialize yourTestbclass. I use the code as follows:Perhaps you do not have error reporting enabled? In any event, you can verify my results here: http://ideone.com/MHP2oX