First OOP script, any pointers?


<?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?

or how else could you do something like this?

thanks.

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>

thanks for that, looks like i need to read in to php5 a little more

using your example - how would i create 2 instances of my box, with different values? surely calling

$css = new createBox(‘500px’, ‘200px’, ‘red’, 1);

would just overwrite my previous object?

i’m probably missing somethng, i literally started reading in to this today, and i think the material i have is a good 3-4 years old.

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?

Yes

so you woudl call your variable something different for another box


$css = new createBox('500px', '200px', 'red', 1);
$cssBox2 = new createBox('700px', '700px', 'purple', 1);

then access it


<div style="<?php echo $cssBox2->displayBox(); ?>"></div>

Personally, (pay attention to that word :wink: ) 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:


    private $_width;
    private $_height;
    private $_color;
    private $_border;

But those are just small things :wink: In the end everybody has their own style