PHP and OOP
Posted: May 15th, 2010 | Author: admin | Filed under: blog | 6 Comments »Hi guys !
Today I’m coming with some very basic OOP tutorial to PHP.
The question is that many people listening about OOP but don’t know why to use it, if in the end, the final product will be the same, same features, same final interface, layout etc…
the question is that many people don’t know that OOP is used in the big companies, and is the most professional way to work with many other programming languages, like a default. by example, Java and .Net.
I’ll show you WHY OOP is more professional and clean. Int the beginning you will think you’re writing more code on OOP, but you will see that will be much more easy to understand your code, you will have less code in your pages, just calling classes and setting methods, and of course, will be much more easy to keep working in new implamentations on your code alone, or with a team of guys working together.
Imagine I’m going to add an user to my website using procedural way. I would have:
$name = "fernando";
$city = "Los Angeles";
$state = "CA";
$zipcode = "87795";
$sql = "insert into users (name,city,state,zipcode) values ('$name','$city','$state','$zipcode');
$gosql = mysql_query($sql) or die ("SQL Error: ".mysql_error());
This example shows in procedural, how this could be done. But imagine that you would need to use this lines in every page that you would need to add an user. I know that to a sign up, this just exist in a unique place. But exist other situations that we do the same thing in many pages, and we copy and paste codes in many time.
My tutorial, will be a very simple example of how to do the same Procedural code above, but using OOP.
If you will like more procedural, no problem. if you work alone, keep with procedural.. but the companies are asking for
OOP
Starting
I’ll have a Class called Users, with the Get and Set methods.
So let’s start.
class User {
var $name; // string
var $city; // string
var $state; // string
var $zipcode; // integer
function setName($name){
$this->name = $name;
}
function setCity($city){
$this->city = $city;
}
function setState($state){
$this->state = $state;
}
function setZipcode($zipcode){
$this->zipcode = $zipcode;
}
function getName(){
return $this->name;
}
function getCity(){
return $this->city;
}
function getState(){
return $this->state;
}
function getZipcode(){
return $this->zipcode;
}
}
This is the basic structure of our class.
Now, imagine you will include a new User on your database, right ? so we need to start our class, and to fill data into it. here we go.
$newuser = new User();
$newuser->setName("fernando");
$newuser->setCity("Los Angeles");
$newuser->setState("CA");
$newuser->setZipcode("87975");
here we inserted data on our class, we can see if our class is with the data correctly, printing the var $newuser on the screen, we will see that:
echo "<pre>"; print_r($newuser);
the code above, return this data:
user Object
(
[name] => fernando
[city] => Los Angeles
[state] => CA
[zipcode] => 87975
)
So here we can see a User Object, with an array, right ? really simple =)
So u tell me: Ok fernando, from now on, I know how to get this data. I can just get it doing that:
$newuser[name] or $newuser[0]
So I say that now, you can access a object data at this way, you need to use this: ->
see here how I would show just the Name data:
echo $newuser->name;
you will see: Fernando
very easy right ?
So when you need to send the new user information to another class or to another method that isn’t inside the User class, you can send the object, and there you get the data.. example:
class Database{
function saveUser($object){
$sql = "insert into Users (name,city,state,zipcode) values ('$object->name','$object->city','$object->state','$object->zipcode')";
}
}
$database = new Database();
$database->saveUser($newuser);
Explaining…
I created a new class, a database class, where we will have a method that save a new user.
When I call this method, I send my Object $newuser that we filled before, and I access the right data properties ready inside my saveUser object.
So here is the whole example code:
Hope it be useful.
class User {
var $name; // string
var $city; // string
var $state; // string
var $zipcode; // integer
function setName($name){
$this->name = $name;
}
function setCity($city){
$this->city = $city;
}
function setState($state){
$this->state = $state;
}
function setZipcode($zipcode){
$this->zipcode = $zipcode;
}
function getName(){
return $this->name;
}
function getCity(){
return $this->city;
}
function getState(){
return $this->state;
}
function getZipcode(){
return $this->zipcode;
}
}
class Database{
function saveUser($object){
$sql = "insert into Users (name,city,state,zipcode) values ('$object->name','$object->city','$object->state','$object->zipcode')";
echo $sql;
}
}
$newuser = new User();
$newuser->setName("fernando");
$newuser->setCity("Los Angeles");
$newuser->setState("CA");
$newuser->setZipcode("87975");
$database = new Database();
$database->saveUser($newuser);
Thanks and until the next !
