I have the following PHP code to describe a color. In short, I used PHP 4 way back in the day and am trying to get my head around 5.5 now so this is the first time I'm really using objects in PHP.
Anyway I have a logic error, that I think has to do with the default value set in the Color class. Could someone please explain why my constructors aren't working, or what's going on?
class Color {
private $red = 1;
private $green = 1;
private $blue = 1;
private $alpha = 1;
public function __toString() { return "rgb(" . $this->red . ", "
. $this->green . ", " . $this->blue . ", " . $this->alpha . ")"; }
}
class RGBColor extends Color {
public function __construct($red, $green, $blue) {
$this->red = $red; $this->green = $green;
$this->blue = $blue; $this->alpha = 1;
}
}
class RGBAColor extends Color {
public function __construct($red, $green, $blue, $alpha) {
$this->red = $red; $this->green = $green;
$this->blue = $blue; $this->alpha = $alpha;
}
public function __toString() { return "rgba(" . $this->red
. ", " . $this->green . ", " . $this->blue . ", " . $this->alpha . ")"; }
}
$c = new Color();
echo "Color: " . $c . "<br>";
$c1 = new RGBColor(0.6, 0.4, 1.0);
echo "RGB Color: " . $c1 . "<br>";
$c2 = new RGBAColor(0.6, 0.4, 1.0, 0.5);
echo "RGBA Color: " . $c2 . "<br>";
I get the following output...
Color: rgb(1, 1, 1, 1)
RGB Color: rgb(1, 1, 1, 1)
RGBA Color: rgba(0.6, 0.4, 1, 0.5)
When I should be getting...
Color: rgb(1, 1, 1, 1)
RGB Color: rgb(0.6, 0.4, 1.0)
RGBA Color: rgba(0.6, 0.4, 1, 0.5)
Thanks! -Cody
$red&c. member variables in the base class looks a bit odd. What if you later want to support HSL and HSLA color values? Will you convert from/to RGB(A) when constructing/outputting the objects? Fair enough if you only want to use RGBA internally, but make certain that you're making the decision rather than falling into it.