Hey Having a little bit of trouble getting my head around the purpose of OOP. It definitely looks cleaner when I see other OOP code but I don't see how its beneficial in any other way. Heres the start of a MySQL class I was working on: <?php error_reporting(E_ALL); class MySQL { private $dbHost; private $dbUser; private $dbPass; private $dbName; public function __construct($dbHost, $dbUser, $dbPass, $dbName) { $this->dbHost = $dbHost; $this->dbUser = $dbUser; $this->dbPass = $dbPass; $this->dbName = $dbName; } /* connect to mysql */ public function connect() { $connection = mysql_connect($this->dbHost, $this->dbUser, $this->dbPass); if($connection) { mysql_select_db($this->dbName) or trigger_error(mysql_error()); } else { trigger_error('Cannot connect to the host.'); } } /* do query */ public function query($query) { $query = mysql_query($query) or trigger_error(mysql_error()); return $query; } } $mysql = new MySQL('localhost','poker_5','5','poker_5'); $mysql->connect(); $q = $mysql->query("SELECT * FROM `namdes`"); while($r = mysql_fetch_array($q)) { echo $r['name'] . ' ' . $r['surname'] . '<br>'; } ?> PHP: Maybe I didn't do something right but my code works so... whats the point when I could just use: <?php mysql_connect("localhost", "username", "password"); mysql_select_db("dbname"); $q = mysql_query("SELECT * FROM `names`"); while($r = mysql_fetch_array($q)) { echo $r['name'] . ' ' . $r['surname'] . '<br>'; } ?> PHP: I'm not saying OOP is pointless, I just want to know how and why it's better and if I'm using it in the correct way.
It's up to purpose, but think that you can do e.g. this : class MySQL { private $dbHost; private $dbUser; private $dbPass; private $dbName; public function __construct($dbHost='localhost', $dbUser='user', $dbPass='pass', $dbName='db_name') { $this->dbHost = $dbHost; $this->dbUser = $dbUser; $this->dbPass = $dbPass; $this->dbName = $dbName; /* connect to mysql still in he construct */ $connection = mysql_connect($this->dbHost, $this->dbUser, $this->dbPass); if($connection) { mysql_select_db($this->dbName) or trigger_error(mysql_error()); } else { trigger_error('Cannot connect to the host.'); } } /* more methods..... */ } And then in PHP, you just call following : $mysql = new MySQL(); //and you're connected $mysql->query("..."); .... There's many advanteges of using classes, just take a look on php.net for more examples. You can fetch all data from db and return standard array, it's better for error handling (you can define your own error method), etc...
Oop is always slower than the traditional procedural, the only real advantages are it's readability and management.
As far as I know OOP is the best way to programming. Why? You can build a large application with small amount of code by re-use your code. Easy to maintain and easy to upgrade your application. Anyway you will never know the power of OOP if you don't dig further.
hmm, this is really an old debate, oop is not an option when you're just creating simple applications, it will start getting significant when you're developing rather large apps
I will try to explain in simple example, Db class , for making a connection and auto closing it. class Db{ private $CON = null; private $conf = null; public function __construct($conf = null){ if($conf) $this->connect( $conf ); else $this->connect( Config::getDbConf() ); } function connect($conf){ $this->conf = $conf; $this->CON = mysql_connect($conf['host'], $conf['user'], $conf['pass']); mysql_select_db($conf['db']); } function query($sql){ if( $this->CON == null ) $this->connect( $this->conf ); return mysql_query($sql , $this->CON ); } function escape($str){ return mysql_real_escape_string($str); } function __destruct(){ if (is_resource($this->CON)) mysql_close( $this->CON ); } } PHP: You can make $CON to be static so you can share across multiple Table classes and this is the reason some OO code is faster than procedural !!! As we have basic class for DB you can make classes that work with specific tables eg class CarsTbl extends Db{ public function __construct(){ parent::__construct(); } public function getCarsByMake( $makeName ){ $makeName = $this->escape( $makeName ); $sql = "select title,price from cars where make = '$makeName' order by price "; $RS = $this->query( $sql ); //parse RS and return array } } PHP: Also very usefull is to have static class/methods eg for configuration class Config { static $conf = null; public static function getDbConf(){ return self::get('db'); } public static function get($confName){ self::_init(); return isset(self::$conf[$confName]) ? self::$conf[$confName] : null; } static function _init(){ if ( self::$conf != null) return ; //parse .ini or some other conf file and pump it to $conf array // though I will here do it manually for easier understaning of code $conf = array(); $conf['db'] = array('host'=>'local' , 'user' => 'lolita' , 'pass' => '69','db'=>'mysitedb'); $conf['ebayapikey'] = '839482938492834'; $conf['default_theme'] = 'purple'; self::$conf = $conf; } } PHP: than use them $make = strtolower( $_GET['make'] ); $carsTbl = new CarsTbl(); $results = $carsTbl->getCarsByMake( $make ); $themeCss = isset($_COOKIE['theme']) ? $_COOKIE['theme'].'.css' : Config::get('default_theme') .'.css'; //include TPLs print results etc ... ... PHP: What you have is some basic classes that you can use across many web sites , and if you need specific for eg Car site you just extend them and write specific methods, so after 2 months you will have lot of base classes , and making a new site will drop development time significantly. You also get a clean code, if you found out that there is bug in some class you just fixing it in one place , generally classes should be short for easier code management etc , there are lot more reasons for OO style, one of the biggest to me is having a complex site with lot of features that would be impossible to manage/change in non OO code, the other strong one is that you can really speed up app if you know how to use OO coding.