Hi all

I would like to challenge a PHP guru to make a decent tutorial on OOP. I have read a ton of tuts on OOP on the net , but for some odd reason the principles described, never seem to work.

I know what OOP programming is all about, I learnded about OOP principels in Pascal 7.0. But never learned to use it in real life. So if someone with real life OOP knowlege is willing to write a tut that is newbee freindly, I will gladly reveiw the tut and ask the "right" questions to the material, until we have a fantastic tut. Maybe we can have the tut as a sticky.

I suggest that we use PHP5 for the tut. I have a book with a sql class, only the book says nothing about how to use it. We could use that class as a starting example, here is the class:

Class mysql{
private $linkID;

private $host;
private $user;
private $pswd;
private $db;

/Class constructor, initializes host etc/
function __construct($host,$user,$pswd, $db){
$this->host = $host;
$this->user = $user;
$this->pswd = $pswd;
$this->db = $db;
}

/connect to mySQL db/
function connect(){
try{
$this->linkID = @mysql_connect($this->host,$this->user,$this->pswd);
if (! $this->linkID)
throw new Exception("Could not connect to mySQL database!");
}
catch (Exception $e){
die($e->getMessage());
}
}

lets have a form from another PHP page contact the class and log in to the database. Like so:

<?php
include "mysql.class.php";
//create new mysql object
$mysqldb = new mysql("host","user","pswd","db");
//connect to database
$mysqldb->connect();
?>

Personally, I have tried the above a dusin times and it has never worked. I hope somebody takes up the challenge.

    I think example classes such as the one above teach you nothing about the benefits of object oriented code. It's basically just encapsulation (and misuse of Exception) which can be done just the same with procedural code and include().

    Any tutorial should start out by explaining the 3 'pillars' of OO - encapsulation, inheritance and polymorphism. If it were to be written with PHP 5 in mind it should explain the uses and benefits of interfaces and abstract classes.

      I totally agree with you. I included the class as an inspration.

        Write a Reply...