Archive
Categories
- Antivirus (4)
- Applications (5)
- Books and Articles (24)
- Computer Tutorials (10)
- Flash (3)
- Game (4)
- Graphic Design Tutorials (13)
- Internet Software (5)
- Internet Tutorials (12)
- Movie (8)
- Music (4)
- Novel Collections (5)
- Photoshop (10)
- Programming Tutorials (12)
- Template (3)
Tutorial : How To Use PHP Classes
11:52 PM |
Diposkan oleh
Plunturan Bersatu |
Edit Post
PHP is an object oriented scripting language. What does that mean? It means that we have a great way to organise our code and a great way to re-use chunks of code.
The whole point of an object oriented language is to make everything better. The main problem is that object oriented PHP is a difficult concept to grasp if you have never used it before.
Why use object oriented PHP at all?
In times gone by before the days of object oriented PHP, everything would be put into functions. Each function would be a chunk of code that completes a task and you call it from elsewhere in your code, you could send it values, variables etc. Now functions are all very well, and are great for organising small projects but when you get into larger projects you will start running into problems.
Object oriented programming really shows its strengths when you start to build bigger, more complex projects. Mass organisation becomes easier and more robust. Teams of developers can operate independently with little fear of interfering with each others variable names. This kind of robustness simply isn’t possible using functions.
How to use object oriented PHP
Object oriented programming revolves around classes. Classes are basically the containers for your functions….Except to make things a bit more complicated the functions aren’t called functions in object orientated PHP, they are called methods.
Classes are created in much the same way as functions are in non object oriented PHP except instead of the keyword ‘function’ we use ‘class’:
<?php
class first{
//do something
}
?>
You can then use your class within your code by creating a new instance of it:
<?php
$result = new first();
?>
Now obviously our class doesn’t do anything because we haven’t added anything to our class but we have created a basic class.
How to make the class do something
In order to make the class do something we need to add a few things to it. We need to add variables(called properties in OOP) that we can use and functions(called methods in OOP) that will do the work for us.
In Object oriented we usually have two standard functions at a minimum. These two properties are the setter and getter functions.
<?php
class first{
var $name;
function set_name($new_name) {
$this->name = $new_name;
}
function get_name() {
return $this->name;
}
}
?>
All this probably looks a bit confusing but its all really quite simple. It all looks confusing because of $this->…..$this is simply a built in variable that points to the current object. PHP knows exactly what to do when it comes across this.
Lets use our PHP class
Now we have a fully functioning class, one that actually does something. So how do we use it? We create an instance of the object in the servers memory:
<?php
$andy = new first();
$steve = new first();
?>
We now have two handles to our class. Both use the same class, just a different instance of it. Seeing this it slowly becomes clear how much simpler your code could become. You can use the same class as many times as you like and just set different handles to each instance and do different things to each instance. For example we can set a different name to each one:
<?php
$andy = new first();
$steve = new first();
$andy-> set_name("Andy Kay");
$steve->set_name("Steven Smith");
?>
Now this code doesn’t actually display anything on the screen but we have set two objects up and set the names of the two objects. It should be noted that $andy and $steve are completely separate objects, the only thing that relates them is that they use the same class but they are completely different objects in the eyes of PHP.
How to get at the results?
When accessing methods and properties of a class, you use the arrow (->) operator. So to get at our names that we set earlier:
<?php
$andy = new first();
$steve = new first();
$andy-> set_name("Andy Kay");
$steve->set_name("Steven Smith");
echo $andy->get_name() . "<br />";
echo $andy->get_name();
?>
These are the basics but as you become more involved you will realise the power of OOP. If you want to get anywhere in PHP you need to be using an object oriented approach.
Label:
Programming Tutorials
Subscribe to:
Post Comments (Atom)
0 komentar:
Post a Comment