On 10 Nov 2009, at 10:46, Maciej wrote:
[snip]
> When I was searching for easy approach to learn OO style programming I
> didn't have that experience which I've got now and now I understand
> why its good way to programing this way ... or at least easier to
> read own codes from let say 4 months ago.
> Can someone please help me to start somehow.
[snip]
Various people have given you links that teach OO programming in PHP. I strongly suggest you should look at some of the theory behind Object Orientation and learn some of the terminology. Specifically look at design patterns http://en.wikipedia.org/wiki/Design_pattern_(computer_science)
OO can be a powerful tool in the programmers arsenal. But it can be a can of worms too. Some of the worst code I have ever seen is poorly written OO code. It was a convoluted mess and extremely difficult to maintain. You don't need an OO language to write OO and similarly you aren't necessarily writing OO when you're using an OO language. OO languages make it easier to write OO but are not a requirement.
Take time. Think about your design. And have fun.
Melanie
This is how I personally would do it in this slightly exaggerated example. Always more than happy to help o7 . I learnt oop when I started playing with Java however, PHP does it quite nicely these days. Best thing for you to do is download some classes (http://www.phpclasses.org/) and implement them / see how they work. If this code needs further explanation just let me know.<?php/**
* @author Jason Leyland
* @copyright 2009
*/class myTable{
/* Set up some variables */
public $tableName;
public $query;
private $error;
private $header = true;
/* Access protected */
function __construct(){
/* This is simalar to document.onLoad */
}
public function getTable(){
global $database;
/* Our query string */
$q = "select * from $tableName where $query";
/* Execute our query using our database class */
$result = $database->query($q);
/* Set up our local return variable */
$output = '<table>';
/* Check the query hasn't failed */
if($database->checkResult !== false){
/* Loop through our data */
while($row = mysql_fetch_assoc()){
$output .= '<tr>';
foreach($row as $key => $value){
/* Construct the table cells */
if($header){
$output .= "<th>$value</th>";
$header = false;
}else{
$output .= "<td>$value</td>";
}
}
$output .= '</tr>';
}
return "$output</table>";
}else{
$this->setError();
return '<p>There has been an unexpected error: '.$this->error.'</p>';
}
}
private function setError(){
$this->error = mysql_error();
}
function __destruct(){
/* This is simalar to document.onUnLoad */
}
}
if(isset($_GET['myvariable'])){
/* Initiate the class */
$myTable = new myTable();
/* Set up some variables */
$myTable->tableName = 'sometable';
$myTable->query = "myvariable = ".isset($_GET['myvariable']);
/* Execute our query */
echo($myTable->getTable());
}else{
echo('<p>There has been an unexpected error</p>');
}
?>