PHP class extends not working why and is this how to correctly extend a class?
Posted
by
Matthew
on Stack Overflow
See other posts from Stack Overflow
or by Matthew
Published on 2011-03-17T00:07:19Z
Indexed on
2011/03/17
0:09 UTC
Read the original article
Hit count: 143
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;
© Stack Overflow or respective owner