PHP class extends not working why and is this how to correctly extend a class?
- by Matthew
Hi so I'm trying to understand how inherteince works in PHP using object oriented programming. The main class is Computer, the class that is inheriting is Mouse. I'm extedning the Computer class with the mouse class. I use __construct in each class, when I istinate the class I use the pc type first and if it has mouse after. For some reason computer returns null? why is this?
class Computer {
protected $type = 'null';
public function __construct($type) {
$this->type = $type;
}
public function computertype() {
$this->type = strtoupper($this->type);
return $this->type;
}
}
class Mouse extends Computer {
protected $hasmouse = 'null';
public function __construct($hasmouse){
$this->hasmouse = $hasmouse;
}
public function computermouse() {
if($this->hasmouse == 'Y') {
return 'This Computer has a mouse';
}
}
}
$pc = new Computer('PC', 'Y');
echo $pc->computertype;
echo $pc->computermouse;