Object Oriented PHP
Object Oriented Programming with PHP can save you a lot of time by utilizing code re-use. Before I coded PHP OO, I remember thinking of it as some BIG ELEPHANT that I wasn't sure I wanted to tackle. The basic concept iss really no big deal and I'm going to demonstrate how easy and practical it is right now.
The first step in Object Oriented Programming is to understand what a class and an object is.
A Class is a set of common data which will get re-used. For example, lets think about a News Article. Articles usually will have a headline, author, story, date, and a link. The Class is just the skeleton, or the schema of an object.
An Object is an instance of a class. In this case, it is easiest to think of a New Article that has been created already. Another way to look at a Class/Object relationship is that You are an instance of the Human Class.
Let's look what an online news page which feeds from a mysql database might consist of if it is programmed OO.
<? // use the keyword 'class' and the name of the class after it class article { // // your class is primarily just a combination of // variables and functions which work together. var $id; var $headline; var $author; var $story; var $date; var $link; // a function with the same name as the class // is called a constructor. this function is triggered // when an object of this class is created. function article($id) { $result=mysql_query("select * from articles where id='" . $id . "'"); // when defining variables that belong to this class, while inside the class, // use 'this->' before the variable name. $this->id = $id; $this->headline = mysql_result($result,0,"headline"); $this->author = mysql_result($result,0,"author"); $this->story = mysql_result($result,0,"story"); $this->date = mysql_result($result,0,"date"); $this->link = mysql_result($result,0,"link"); } function printarticle() { echo "<table border=1 cellspacing=0 cellpadding=0> <tr><td>" . $this->headline . " by " . $this->author . "</td></tr> <tr><td>" . $this->story . "</td></tr></table>"; } } ?>
Now that we have created a class, we need to create an instance of the class. ( the object ) This tutorial is just to show you how OO works with PHP, so we will assume there is a record in the database already which has an 'id' value of '1'.
<? // create a new instance of the news article class. // the argument '1' below is sent to the Constructor function // of the class. $my_article = new article(1); // !! That's It! You created an instance of your class! // now to print it to your browser, just call the printarticle() function $my_article->printarticle(); // you could call the constructor again if you want to reset the article $my_article->article(2); $my_article->printarticle(); // then if you wanted to keep the info in $my_article, you can simply // create a new article object al together. $new_article = new article(3); $new_article->printarticle(); ?>
I hope this has been informative. There's a lot more to OO, but this was just meant to give you a taste.
You can read more about OOP here: http://www.php.net/oop
Post new comment