PDA

View Full Version : PHP -Procedural or Object Oriented


stoneygossard
03-28-2007, 09:29 PM
Hi Guys,

I am still on the php trip but am increasingly seeing "classes" within code snippets on the forums.

Does anyone know if the object oriented php is taking off or is it the way forward? I have heard some arguements for and against the oo version. Anyone with some decent info on this?

stoney

davidj
03-29-2007, 09:14 PM
hi mate

im back from london

ok..

dont think of OOP as more advanced

OOP is a method which keeps your code in one area (in objects)

Its tricky to get your head around and requires a completly different way of programming

stoneygossard
03-29-2007, 09:23 PM
hi mate

im back from london

ok..

dont think of OOP as more advanced

OOP is a method which keeps your code in one area (in objects)

Its tricky to get your head around and requires a completly different way of programming

Thanks David,

I actually studied a lot of Java which is OOP but didn't like the whole mindset, I much prefer procedural. So I think I'll stick with it. Do you have a preference yourself?
Also, is the whole programming world gonna go OO in the future making it worthwhile to concentrate on OO or is that a non-runner?

Stoney

davidj
03-29-2007, 10:24 PM
its good when you want to avoid variable mismanagement

if your coding with a collegue there is a risk you both could start setting the same variable names. With classes it doesnt matter as they are just relevent to the class.

Also its supposed to make your code reusable so you could create a connection class and use that across multi apps...

stoneygossard
03-29-2007, 11:21 PM
its good when you want to avoid variable mismanagement

if your coding with a collegue there is a risk you both could start setting the same variable names. With classes it doesnt matter as they are just relevent to the class.

Also its supposed to make your code reusable so you could create a connection class and use that across multi apps...

David,

U don't seem to be bitten by the oop but? Am I right? Tell me what I wanna hear man, procedural is gonna be here forever!

Stoney

davidj
03-30-2007, 06:39 AM
only because i havnt really been exposed to it

maybe after a couple of projects i will learn to like it

davidj
03-31-2007, 09:14 PM
here is an example i knocked up


<?php

class tables{

var $table;
var $times;
var $display;

function tables(){

$this->display="<table border=\"1\">\n";

for($this->table=1; $this->table < 13; $this->table++){
$this->display.= "<tr><td>this is the ".$this->table." times table</td></tr>\n";
for($this->times=1; $this->times<13; $this->times++){

$this->display.= "<tr><td>".$this->times." x ".$this->table." = ".$this->times*$this->table."</td></tr>\n";

}

if($this->table < 12){

$this->display.= "</table>\n<table border=\"1\">\n";

}else{

$this->display.= "</table>\n";

}
}
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<?
$obj = new tables();
echo $obj->display;
?>
</body>
</html>


you got me interested in it now

stoneygossard
03-31-2007, 09:16 PM
David,

I can see you are a competent programmer. How did u enjoy that piece of code?I find it easier on the brain to user procedural all the same.

Stoney

davidj
03-31-2007, 09:37 PM
if all your code is in one area or a class then i can see the benefit. Its not for every task but if you were in an OOP environment it is the way to go

lets examine what i did...

a class is defined by...


class tables{

//stuff here

}


then all your routines or events are function driven so you could have a connections function and a select function and an insert function in the same class

variables are confined to a class so if you had 2 vars called $variable in 2 classes they wouldnt clash.

you have to declare a variable in the class before you can access it in a function like this...


var $table;
var $times;
var $display;


to access and set a var within a function just use this method...
$this->display //<< accesses $display

everything else is the same. There is some var type declarations in PHP5 like private public which means that some vars cant be accessed directly and need functon calls to work so by setting private vars means another developer cant use that var using a straight call

to call the class and create an Object just do...


$obj = new tables(); //<< call class and function and set result to an object called $obj.
echo $obj->display; //<< do what you like with the object like echo.

stoneygossard
03-31-2007, 09:40 PM
Oh i understand it ok David,

As u said, it's a different mindset. Anyway, i'll keep studying the original php for now and if needs be i'll move into object oriented down the line.
thanks
Stoney