I have built lots of PHP websites and I have been using an Object Oriented approach for nearly three years now. I would never look back to purely procedural work. There's just so much to be gained from OOP.
Think of this, and I'll provide some example code...
Say you have a small website that hosts cooking tips for students and on your first page you want to list the first 10 tips with a link to show all tips.
On a procedural level, you going to use a LOT of code over and over again to do these simple tasks, but in a OOP based system, you can create a "Tips" class that has all the functions needed to create, edit, retreive and delete your tips which can be called when you need it.
For the database, lets assume our table is called 'tips' and all it has in it is 'title', 'description' & 'content' columns.
Let's create a master "settings.php" file. This file will include PEAR DB for all your database interaction, it will set any configuration and will also include your "Tips" class that I'll cover after this.
settings.php
// Include PEAR::DB
require_once('DB.php');
// Database configuration settings
$dbconfig = array(
'phptype' => 'mysql',
'hostspec' => 'localhost',
'database' => 'my_database',
'username' => 'my_username',
'password' => 'my_password'
);
// Connect to the database
$db = &DB::connect($dbconfig);
$db->setFetchMode(DB_FETCHMODE_ASSOC);
// Include & Instantiate Tips class
require_once('Tips.php');
$tips =& new Tips($db);
?>
Now for the "Tips" class. I'll give you an example of how OOP principles can save you a LOT of time when doing simple things like retrieving a list of tips, or just a single tip.
Tips.php
class Tips {
/**
* Database connection
* @access private
* @var object
**/
var $db;
/**
* Tips constructor
* @param object instance of database connection
* @access public
**/
function Tips (&$db) {
$this->db =& $db;
}
/**
* Returns tips as array
* @return array tips
* @access public
**/
function getTips() {
$sql="SELECT * FROM tips ORDER BY created";
$tips = $this->db->getAll($sql);
return $tips;
}
/**
* Returns latest 10 tips as array
* @return array tips
* @access public
**/
function getLatestTips() {
$sql="SELECT * FROM tips ORDER BY created LIMIT 0, 10";
$tips = $this->db->getAll($sql);
return $tips;
}
/**
* Given a tip id, returns the tip as an array
* @param int tip id
* @return array tip details
* @access public
**/
function getTip($id) {
$sql="SELECT * FROM tips WHERE id='".$id."'";
$result = $this->db->query($sql);
if ( $result->numRows() == 1 ) {
return $result->fetchRow();
} else {
return false;
}
}
}
?>
I've stuck to static class methods here for illustration purposes. Lets get your first page set up.
index.php
Cooking tips for students
$tipList = $tips->getLatestTips();
foreach($tipList as $tip):
?>
Now lets tackle the "view.php" page.
include_once('setting.php');
$tip = $tips->getTip($_GET['id']);
?>
Cooking tips for students:
I hope that has given you some insight into how Object Oriented Programming can save hassle and time.
2006-11-23 11:52:13
·
answer #1
·
answered by Poncho 3
·
1⤊
0⤋
uhm, I think you are asking if Object Oriented Programming is better than simple php code? Well, yeah, but depends on what you are doing. I don't use PHP, but I'm not sure if you can use inheritence in PHP either. Classic asp isn't even considered OOP
definition of oop
A style of programming that supports encapsulation, inheritance, and polymorphism. Some languages are inherently object oriented (eg, Smalltalk and Java) while other languages support object oriented extensions (eg, C++ and Visual Basic). It is often stated that Visual Basic is not truly an object oriented language because it doesn't support inheritance. If you make that statement then you have to say that COM is not object oriented either, because COM does not support inheritance either. ...
2006-11-23 00:52:19
·
answer #2
·
answered by comn8u 4
·
0⤊
0⤋
In OOPS website we have much flexibility to customize the code.
Simple PHP coding it is very difficult to to fix the issues.
OOPS also helps the re usability of the code.
2014-12-08 20:55:31
·
answer #3
·
answered by Dave 3
·
0⤊
0⤋
i have coded a few websites with PHP, and have never used any real oop features, like inheritence. Its just as easy to make and maintain a easily coded website will tons of features (signing in for customers, searching, e-commerce) , as it is using an oop approach for a website.
2006-11-23 07:10:16
·
answer #4
·
answered by agent69akasexy 2
·
0⤊
0⤋
It's a difficult judgment to make - you'd need a site that does exactly the same thing (same inputs, same outputs, everything identical except code) in both paradigms to make a fair comparison.
Rawlyn.
2006-11-23 00:41:38
·
answer #5
·
answered by Anonymous
·
0⤊
0⤋
You probably wont be able to notice the difference at run time, especially with the latest Zend engines.
2006-11-23 03:06:28
·
answer #6
·
answered by watsonc64 3
·
0⤊
0⤋
Please find the oops details here - http://gopaldas.org/core-java/java-object-oriented-programming-concepts-oops/
2014-02-21 06:52:03
·
answer #7
·
answered by Gopal 1
·
0⤊
0⤋