Contributed by Marcus Cavalcanti
I saw your article about OO examples in many languages and I would like to contribute in a new version of PHP implementation.
The current OO PHP implementation in your article does not include the new OO features in PHP 5 (or >) version, so I made an example with a new implementation based in the original problem.
My implementation has 5 files: the interface (Shape.php), the classes (Circle.php and Rectangle.php), the script to run/test the problem (main.php) and finally the results output (output.txt).
If you have problems with the files, please contact me.
Regards,
Marcus Cavalcanti
www.marcuscavalcanti.net
<?php
interface Shape {
public function Draw();
public function MoveTo($x, $y);
public function RMoveTo($dx, $dy);
}
?>
<?php
class Rectangle implements Shape {
// If a method can be static, declare it static. Speed improvement is by a factor of 4. (http://vega.rd.no/article/php-static-method-performance)
private static $width;
private static $height;
private static $x;
private static $y;
function __construct ($x, $y, $width, $height) {
self::$x = (float) $x;
self::$y = (float) $y;
self::$width = (float) $width;
self::$height = (float) $height;
}
public function Draw () {
echo sprintf("Drawing a Rectangle at %d, %d, width %d, height %d<br>", self::$x, self::$y, self::$width, self::$height);
}
public function MoveTo ($x, $y) {
self::$x = $x;
self::$y = $y;
}
public function RMoveTo ($dx, $dy) {
self::$x += $dx;
self::$y += $dy;
}
public static function __set ($var, $val) {
self::$$var = (float) $val; // note the '$$'
}
public static function __get ($var) {
return self::$$var; // note the '$$'
}
}
?>
<?php
class Circle implements Shape {
// if a method can be static, declare it static. Speed improvement is by a factor of 4. (http://vega.rd.no/article/php-static-method-performance)
private static $radius;
private static $x;
private static $y;
function __construct($x, $y, $radius) {
self::$x = (float) $x;
self::$y = (float) $y;
self::$radius = (float) $radius;
}
public function Draw () {
echo sprintf("Drawing a Circle at %d, %d, radius %d<br>", self::$x, self::$y, self::$radius);
}
public function MoveTo ($x, $y) {
self::$x = $x;
self::$y = $y;
}
public function RMoveTo ($dx, $dy) {
self::$x += $dx;
self::$y += $dy;
}
public static function __set ($var, $val) {
self::$$var = (float) $val; // note the '$$'
}
public static function __get ($var) {
return self::$$var; // note the '$$'
}
}
?>
<?php
// load the Circle and Rectangle classes and Shape interface
function __autoload ($className) {
require $className . '.php';
}
// set up an array with some shape instances
$shapes = array(new Rectangle(10, 20, 5, 6), new Circle(15, 25, 8));
// iterate through the shapes
for ($i = 0; $i < 2; $i++) {
$shapes[$i]->Draw();
$shapes[$i]->RMoveTo(100, 100);
$shapes[$i]->Draw();
}
echo "<br><br>";
// circle specific functions
$circle = new Circle(10, 20, 30);
$circle->Draw();
$circle->radius = 100; // set new value with __set "magic method"
$circle->Draw();
$circle->MoveTo(200, 200);
$circle->Draw();
echo "<br><br>";
// rectangle specific functions
$rectangle = new Rectangle(10, 20, 30, 40);
$rectangle->Draw();
$rectangle->width = 100; // set new value with __set "magic method"
$rectangle->height = 195; // set new value with __set "magic method"
$rectangle->Draw();
$rectangle->RMoveTo(10, 10);
$rectangle->Draw();
?>
Drawing a Rectangle at 10, 20, width 5, height 6 Drawing a Rectangle at 110, 120, width 5, height 6 Drawing a Circle at 15, 25, radius 8 Drawing a Circle at 115, 125, radius 8 Drawing a Circle at 10, 20, radius 30 Drawing a Circle at 10, 20, radius 100 Drawing a Circle at 200, 200, radius 100 Drawing a Rectangle at 10, 20, width 30, height 40 Drawing a Rectangle at 10, 20, width 100, height 195 Drawing a Rectangle at 20, 30, width 100, height 195