Jul 172012
Programming in php: Inheritance not working in PHP on newest questions tagged php – Stack Overflow
I have got this structure:
abstract class Android {
public function __construct() {
}
public abstract function cost();
public function get_model(){
return "unkown model";
}
}
class HTC extends Android {
public function __construct() {
parent::__construct();
$this->model= "HTC";
}
public function get_model() {
return $this->model;
}
public function cost() {
return 500;
}
}
abstract class FeaturesDecorator extends Android {
public function __construct() {
parent::__construct();
}
public abstract function get_description();
}
class VideoCamera extends FeaturesDecorator {
private $android;
public function __construct($android) {
parent::__construct();
$this->android=$android;
}
public function get_description() {
return $this->android->get_model()." plus a video camera, ";
}
public function cost() {
return 200 + $this->android->cost();
}
}
class BigScreenSize extends FeaturesDecorator {
private $android;
public function __construct($android) {
parent::__construct();
$this->android=$android;
}
public function get_description() {
return $this->android->get_model()." plus a big size screen, ";
}
public function cost() {
return 100 + $this->android->cost();
}
}
After applying the following pattern:
$android2 = new HTC();
$android2 = new BigScreenSize($android2);
$android2 = new LongLastingBattery($android2);
echo "<br/><br/>You have bought ".$android2->get_description()." for $".$android2->cost();
I get
You have bought Unkown brand plus a long lasting battery, for $650
Again:
I wonder why I get Unknown brand instead of HTC from get_model() called by get_description(), while $650 is being calculated correctly.
See Answers
source: http://stackoverflow.com/questions/11511496/inheritance-not-working-in-php
Programming in php: programming-in-php
Recent Comments