<?php
/* A class to define the style property of a div in css */
class createBox {
var $width;
var $height;
var $color;
var $border;
function makeBox($x,$y,$hex,$isBorder) {
$this->width=$x;
$this->height=$y;
$this->color=$hex;
if($isBorder) {
$this->border='border:1px solid #000;';
}
}
function displayBox() {
return 'width:'. $this->width . '; height: ' .
$this->height . ';background-color:' .
$this->color . ';' . $this->border;
}
}
/* the magic */
$css = new createBox;
/* create a div 500px by 200px,
#d4d4d4 as BG with a border */
$css->makeBox('500px','200px','#D4D4D4',1);
?>
<html>
<body>
<div style="<?php echo $css->displayBox(); ?>">
</div>
</body>
</html>
haven’t looked at oop really before although i have some (limited) experience in c++
whilst basic, that does appear to work. is there anything wrong with it in terms of structure, calls, returning values etc?
As far as I am familiar with OOP specially in PHP, there are two types of OOP structures depending upon the PHP Version. Some traditional OOP structure are written for PHP 4 and PHP 5 has come up with lots of real OOP features implemented. So your code seems absolutely fine but written for PHP 4. Your class can be rewritten in PHP 5 as follows:
<?php
/* A class to define the style property of a div in css */
class createBox {
private $width;
private $height;
private $color;
private $border;
public function __construct($x,$y,$hex,$isBorder){
// this is the class constructor
// the constructor for PHP 4 will be the name of the class with public keyword
$this->width = $x;
$this->height = $y;
$this->color = $hex;
if($isBorder) {
$this->border = 'border:1px solid #000;';
}
}
public function displayBox() {
return 'width:'. $this->width . '; height: ' .
$this->height . ';background-color:' .
$this->color . ';' . $this->border;
}
}
/* the magic */
$css = new createBox('500px', '200px', '#D4D4D4', 1);
/* create a div 500px by 200px,
#d4d4d4 as BG with a border */
?>
<html>
<body>
<div style="<?php echo $css->displayBox(); ?>"></div>
</body>
</html>
Yes I would also recommend you to go for some new books having some good examples of at least PHP version 5. Actually you will find more powerful features with PHP5 specially in case of OOP.
$css = new createBox(‘500px’, ‘200px’, ‘red’, 1);
would just overwrite my previous object?
Personally, (pay attention to that word ) I would name your class and method different.
For example:
$css = new createBox();
to:
$css = new css_box();
Because the “new” keyword already indicates the creation, so you con’t need to call your class createBox anymore. I added the css_, because a box can be so much more, and could be confusing when you are developing further and larger projects.
Also the method:
$css ->displayBox();
could be
$css ->get_box_style();
because you are not actually displaying something in that method, you are returning something.
Last but not least:
Most programmers have the convention of starting private methods and properties with an “_” so that later it is clear if it can be used outside the object or not: