is this how you do OO programming?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

crazytopu
Forum Contributor
Posts: 259
Joined: Fri Nov 07, 2003 12:43 pm
Location: London, UK
Contact:

is this how you do OO programming?

Post 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
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

nope, it looks good to me, but then again what do i know
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post 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.
theda
Forum Contributor
Posts: 332
Joined: Sat Feb 19, 2005 8:35 am
Location: USA

Post 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
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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();
dreamline
Forum Contributor
Posts: 158
Joined: Fri May 28, 2004 2:37 am

Post 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
theda
Forum Contributor
Posts: 332
Joined: Sat Feb 19, 2005 8:35 am
Location: USA

Post by theda »

Not to take away from this thread, but what does -> equal in english words?
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post 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.
theda
Forum Contributor
Posts: 332
Joined: Sat Feb 19, 2005 8:35 am
Location: USA

Post by theda »

So it's kind of like property ^_^?
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post 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()
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post by pilau »

HTML guided OOP looks awesome in PHP. It can be done to any kind of HTML code, right?
crazytopu
Forum Contributor
Posts: 259
Joined: Fri Nov 07, 2003 12:43 pm
Location: London, UK
Contact:

Post 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
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post by pilau »

Wow, well that explain a lot!
theda
Forum Contributor
Posts: 332
Joined: Sat Feb 19, 2005 8:35 am
Location: USA

Post 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.
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post 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.
Post Reply