Jump to content

Learning OOP


deansatch

Recommended Posts

I have been using php for a long time now and I am know it very well. I feel it is time I learned OOP though. I have been reading up on it and if I understand correctly, it allows total separation of html and php backend stuff.

 

I am however having trouble finding examples or tutorials that show OOP in a real world useful way.

 

e.g.

 

I have a simple while loop output in to a table which I currently do like this:

 

$v_list ="<h2>Variable List</h2>\n
<div><table><thead><tr><td>V 1</td><td>V 2</td><td>V 3</td></tr></thead><tbody>\n";
$query = mysql_query("SELECT * from table");
$vnum=0;
$vnum = mysql_num_rows($query);
while($row = mysql_fetch_assoc($query)){;
$v1= $row['v1'];
$v2= $row['v2'];
$v3= $row['v3'];

$v_list .= "<tr><td>$v1</td><td>$v2</td><td>$v3</td></tr>\n";

}
$v_list .= "</tbody></table></div>";
if($vnum> 0){echo $v_list;}

 

How could I use OOP to keep the php part in a separate file from the html?

 

e.g.

 

file 1:

my php code looping and setting of variables

 

file 2:

<h2>Variable List</h2>

<div>
<table>
<thead>
<tr><td>V 1</td><td>V 2</td><td>V 3</td></tr></thead><tbody>
<tr><td>$v1[0]</td><td>$v2[0]</td><td>$v3[0]</td></tr>
<tr><td>$v1[1]</td><td>$v2[1]</td><td>$v3[1]</td></tr>
<tr><td>$v1[2]</td><td>$v2[2]</td><td>$v3[2]</td></tr>
</tbody>
</table>
</div>

 

Thanks in advance

Link to comment
Share on other sites

it allows total separation of html and php backend stuff.

 

While OOP can assist in this area, its far from its main objective, so I think you may have been given a bum steer there.

 

If you want to learn OOP, I would suggest picking up one of the bigger frameworks and building a few projects with it. These usually impliment a few different design patterns including MVC.

 

Zend Framework is pretty darn popular atm.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.