Page 1 of 2

is this how you do OO programming?

Posted: Tue Jul 26, 2005 5:17 am
by crazytopu
Hi,

if you take a look at the following code, you'd notice that i have declared a class named table.

Code: Select all

<?php 
#
#
#
#version 1.0
#@crazytopu
#

$pageLayout = new PageLayout();


class PageLayout{


//====INTERNAL VARIABLES=========================================


private $page_title='SoftBangla Business Automation Ltd';
private $css_file_href='main.css';
private $greeting_message= 'THE FUTURE PERSPECTIVE OF IT SERVICES';
private $text_line='';




//====CONSTRUCTOR=================================================
function __construct(){
$this->buildLayOut();

}

//===PUBLIC METHODS===============================================
public function buildLayOut(){

$this->buildBeginingHtml();
//$container= new Table('10%',500,1,left,'#ff0000');
$this->buildEndingHtml();
}

//====PRIVATE METHODS=============================================
private function buildBeginingHtml(){

echo "<html>";
echo "<head>";
echo "<title>".$this->page_title."</title>";
echo "<link rel='stylesheet' type='text/css' href= '$this->css_file_href' ";
echo "</head>";
echo "<body>";
echo "<div id='topbar1'> </div> ";
echo "<div id='topbar2'> </div> ";
echo "<div id='container'> ";

echo "<div id='greetingbar'>"; 


echo "<span class='greetingText'>$this->greeting_message</span>   </div>";

echo "<div id='image'> </div>";
echo "<div id='bottombar'>  </div>";
echo "</div>";

echo "<div id='footer' > </div>";

}


private function buildcontent(){
$this->text_line.='Hello';

echo "";

}



private function buildEndingHtml(){
echo '</body></html>';

}

}



class Table{

//=====INTERNAL VARIABLES=============================================

private $border='0';
private $cellspacing='0';
private $cellpadding='0';
private $width='0';
private $height='0';
private $alignment='';
private $bgcolor='white';

//==== CONSTRUCTOR=====================================================

function __construct($w, $h, $b,$align,$bgclr){

$this->width=$w;
$this->height=$h;
$this->border=$b;
$this->alignment=$align;
$this->bgcolor=$bgclr;
echo "<TABLE BGCOLOR='$this->bgcolor' ALIGN='$this->alignment' CELLSPACING='$this->cellspacing' CELLPADDING='$this->cellpadding' BORDER='$this->border' WIDTH='$this->width' HEIGHT='$this->height'>  ";


}

//=====PUBLIC METHODS==================================================
public function setBorder($n){
$this->border=$n;
}

public function setCellPadding($n){
$this->cellpadding=$n;
}

public function setCellSpacing($n){
$this->cellspacing=$n;
}

public function setWidth($n){
$this->width=$n;
}

public function setHeight($n){
$this->height=$n;
}

}

?>

So, when i want to instanciate an object of that class i write:

Code: Select all

$container= new Table('100%',500,1,center,'#ff0000');

that produces the following html code:

Code: Select all

&lt;TABLE BGCOLOR='#ffOOOO' ALIGN='center' CELLSPACING='0' CELLPADDING='0' BORDER='1' WIDTH='100%' HEIGHT='500'&gt;
and then what? you need to put some content in between "<table> </table>" tags right?


So I added some contents right after the object declaration code:

Code: Select all

$container= new Table('100%',500,1,center,'#ff0000');

//my html contents go here
//more content
//more content

//now i need to close the table object so the </table> tag appears. 

//so what do i do? i call the function close? 

$container.close();  //lets assume the close function returns "</table>"

Okay, do you see any reason why I should do all these hard work to do OO programming?
What if i just typed </table> instead of writing a class and declaraing an object and then calling a function of that object?

Am I on the right track? Do you make all html objects each a distinct class? i.e table class, div class etc.

Is it logical? or it's better to do some part of programming using OO technique and some part of procedurally?

And any general guidelines as to how you use OO techniques to build a web site? Did i do something wrong so far in my code?



Thanks

Posted: Tue Jul 26, 2005 5:26 am
by shiznatix
nope, it looks good to me, but then again what do i know

Posted: Tue Jul 26, 2005 6:19 am
by pilau
It looks so awesome. How come didn't I think of that?
Maybe because I have no idea what OO programming is.
Still I understood your code. And it's brilliantly and practically great.

Posted: Tue Jul 26, 2005 7:33 am
by theda
OO Programming is Object-Oriented programming... More commonly done with C/++

Oh yeah! Here's a link to a nicer definition http://www.webopedia.com/TERM/O/object_ ... g_OOP.html

Posted: Tue Jul 26, 2005 7:36 am
by timvw
You would probably need a function that allows you to addRows to the table... And meaby if you end up with Row classes, you might want a addCell too...

So i would remove the echo calls from the container... And only echo stuff when you call a "render" method...

Meaby i would remove the echo completely, and simply return a $string when render is called... Than you can do even more processing on the generated html table... (For example, feed the $string to a $page->setBody)

Code: Select all

class Table {
  ...

  function addRow($row);
  function addRows($rows);
  function render();
}
This way you end up with code like..

Code: Select all

$rows = array();
while ($row = mysql_fetch_assoc($rs)
{
  $rows[] = $row;
}

$table = new Table(....);
$table->addRows($rows);
$table->render();

Posted: Tue Jul 26, 2005 7:40 am
by dreamline
Well from what i've seen it looks good, way better than what i've coded sofar.. I am new to OOP and still need to get into it, but if i look at some other code done in OOP then i will choose OOP, since my code is very hard to handle right now..

So this code is a nice turorial for me to finally start OOP-ing.. :D

Posted: Tue Jul 26, 2005 7:48 am
by theda
Not to take away from this thread, but what does -> equal in english words?

Posted: Tue Jul 26, 2005 8:50 am
by Roja
theda wrote:Not to take away from this thread, but what does -> equal in english words?
In simplest possible terms, "OF".

$class->method(); = Method of Class.
$class->property; = Property of Class.

Posted: Tue Jul 26, 2005 8:51 am
by theda
So it's kind of like property ^_^?

Posted: Tue Jul 26, 2005 9:14 am
by Roja
theda wrote:So it's kind of like property ^_^?
No. Read it again. It refers to EITHER properties OR methods. Its "OF". As in:

$something->else(); // Else is a method OF something.
$something->foo; // Foo is a property OF something.

If you need further clarification, do it via pm, we're way off-topic.

d11wtq | Removed parens from foo()

Posted: Tue Jul 26, 2005 11:56 pm
by pilau
HTML guided OOP looks awesome in PHP. It can be done to any kind of HTML code, right?

Posted: Wed Jul 27, 2005 2:09 am
by crazytopu
pilau said

Code: Select all

HTML guided OOP looks awesome in PHP. It can be done to any kind of HTML code, right?
with my little knowledge abt OO programming in PHP, i would say "YES".

Those of you who are looking to learn more about OO web design philosophy, this link might be of some help:

http://www.tanguay.at/installPhp5.php5? ... 6aeda8fae6

Posted: Wed Jul 27, 2005 2:24 am
by pilau
Wow, well that explain a lot!

Posted: Wed Jul 27, 2005 8:32 am
by theda
What is the use of OOP if all you're doing is a few this->here stuff? I'm not seeing the logic in the need for OOP, unless just for learning purposes.

Posted: Wed Jul 27, 2005 8:34 am
by pilau
Professional forums such as this one and vBulletin are generated by OO classes.
Don't you think it's better than jusr printing out the code?
Much more flexible.