Jump to content
xisto Community
Sign in to follow this  
Rigaudon

Php: Object Oriented Programming Prerequisites: A good understanding of PHP

Recommended Posts

Hello to all.

This tutorial is for the people who know the basics of PHP and want to take their website/programming skills to the next level.
To be clear: it is advised that those who don't know PHP not read this, as it will be very confusing.

I will be discussing OOP (Object Oriented Programming) here. This is a GREAT prerequisite to Java if you want to learn it and know PHP.

What is OOP?

OOP, or Object Oriented Programming, is not much different from programming normally, but really separates the man from the boys and help your code to be MUCH more organized.

The general idea behind OOP is to wrap everything that is related into one class, which we can then instantiate. You absolutely MUST learn some concepts of OOP before starting Java if you don't want to be completely lost when you start learning it, though I understand some of you probably just want to stick with HTML.

I like to use an analogy for OOP- it's like making a machine to do something for you.

For example, if you wanted to chop down a forest for a new supermall, would you chop down each tree individually? No, you would make a machine that can chop everything down for you. In this case, it's easier to make the machine than to chop down every tree.

On the other hand, let's say you wanted to say hi to your mom. Would you call her up and say hi, or would you make a robot to do it for you? Obviously, you would call her up (unless you really hated her) and tell her because doing so is much easier than making a robot.

In other words, it makes difficult things easier and easier things more difficult.

Anyways, let's jump right in!

Properties of a class
Example
Let's say you're running a furniture making factory. You want to keep track of how many pieces of furniture there are, their status, price, etc.

How would you do this? Hm, well I guess you could store everything into an array:

$furniture1['price'] = "499.99";
$furniture1['status'] = "Available";
linenums:0'>$furniture1 = array();$furniture1['name'] = "Bed";$furniture1['price'] = "499.99";$furniture1['status'] = "Available";
By reading the above code, I hope you can grasp how tedious that would be, and to do maybe 10,000 pieces of furniture a day would be just torture, even if you used mySQL (which I won't be covering here, but probably will in future).

No, this is where OOP comes in. To define a class, simply do:
class furniture{}
And this would make a class called furniture.
Now, let's wrap our code in it. What did we have for furniture? The name, the price, and the status. Let's give the class these attributes.
class furniture{  var $name;  var $price;  var $status;}

The codeword var defines an attribute. There are other keywords specific to OOP (public, protected, private), but that's a little more advanced and won't be covered here.

So, what have we just done? We gave the furniture class three attributes. Simple enough.

Classes can also have functions in them, but when functions are in classes, they're called "methods". All classes should have one never-changing method: the constructor.

The constructor is called every time the object is instantiated, or made. What do we want to do when a furniture is made? Well, we want to give it its name, price, and status. The construct method is: "function __construct()" with 2 underscores and it can take inputs. Here's what we would do:
class furniture{  var $name;  var $price;  var $status;  function __construct($name,$price,$status){     $this->name = $name;     $this->price = $price;     $this->status = $status;  }}
Let's go over what we just did in detail.

We made the __construct() function that takes 3 parameters. In OOP, there is a special variable called "$this" which refers the the executing object, or the object that's running the code.

To get an attribute of a class, we use an arrow (->) from the object to the attribute name.
Here, we set the executing object's name, price, and status to whatever the input is.

There's also a destruct function (__destruct()), but I hardly ever find a use for it.

Obviously, classes can have more than these two functions. If the class had been a washing machine, we would probably add a method called "washClothes()". It just depends on what you want to do.

To call functions of an object, the syntax is the same. You'd put the object, arrow(->), and the method name, like "$washingMachine->washClothes();".

We just went over how to define a class. Let's see it in action now.

Instantiating objects
Instantiating is just a fancy word for "make it". Since we already made a furniture class, let's do that one.
Let's say I wanted to make 3 different pieces of furniture: a bed, a table, and a sofa. This is how we would do that (make sure you've included the class in the PHP file or require()'d it):
$sofa = new furniture("Sofa",1299.99,"Available");$bed = new furniture("Bed",1549.99,"Shipping");$table = new furniture("Table",599.98,"Broken");

This obviously is much easier than what we did before. OOP in conjunction with mySQL can be extremely useful, as you can see.
Of course, in practical use, you'd use variable variables to dynamically generate thousands of furnitures. If I only wanted the status of a specific table, I'd simply do:
echo $table->status;

That's OOP in a nutshell. There's much more complicated stuff, but before I leave, I'd like to leave you with an extremely practical piece of code for sessions:
			$this->id = $_SESSION['id'];
$this->username = $_SESSION['username'];
$this->log_in();
if(!isset($_SESSION['admin'])){
$_SESSION['admin'] = 0;
$this->admin = 0;
}else{
$this->admin = $_SESSION['admin'];
}
}
else{
$this->logged_in=false;
}
}

public function log_in(){
if(isset($_SESSION['id'])){
$this->logged_in = true;
}
}

public function log_out(){
$this->logged_in = false;
$_SESSION = array();
session_destroy();
header("Location linenums:0'><?phpclass Session{ public $id; public $username; public $logged_in = false; public $admin = false; public function is_logged_in(){ return $this->logged_in; } function __construct(){ session_start(); if(isset($_SESSION['id'])){ $this->id = $_SESSION['id']; $this->username = $_SESSION['username']; $this->log_in(); if(!isset($_SESSION['admin'])){ $_SESSION['admin'] = 0; $this->admin = 0; }else{ $this->admin = $_SESSION['admin']; } } else{ $this->logged_in=false; } } public function log_in(){ if(isset($_SESSION['id'])){ $this->logged_in = true; } } public function log_out(){ $this->logged_in = false; $_SESSION = array(); session_destroy(); header("Location: login.php"); }}$session = new Session();?>
This keeps track of a user's id, username, and whether they're logged in or not/admin status. Since (I would think) most people use session for their websites, wrapping it all up in a nice class like this really helps.

I hope you understood what we just did and apply it to your own code to make it more organized. This truly does make you a better PHP programmer, and, as previously stated, is a great prerequisite for Java or any other OOP language.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.