Object-oriented Programming Techniques With Php
Introduction
This tutorial starts from my experience watching my friends at university
the learning object-oriented programming. But they did not understand the way
what & how the object-oriented programming. This happened to me
probably due to too many theories but little or even no example
concrete so that they are confused.
So in this tutorial I tried to give an explanation as possible
and more with concrete examples so it is easy to understand. This tutorial
introduce you to the object-oriented programming (Object Oriented
Programming) by using PHP5.
This tutorial requires a lot of improvement, therefore, if you have
questions or input for the improvement of this tutorial, please send an email to author
the gerrysabar (at) gmail.com. Input you provide is very useful
for the development of this tutorial to better & useful.
What is OOP?
Or object-oriented programming in English is called the Object
Oriented Programming (hereinafter abbreviated as OOP) is a programming technique that
using the object. This object-oriented programming has the ability to
hide what is not important for its users. In this short tutorial
I use PHP5 because PHP5 has a feature-oriented programming
objects are more complete than previous versions.
Why should use OOP?
If you make a small-scale program of course the obvious benefit of this OOP
less felt. But when a project program medium to large scale
it will show how important the use of OOP.
Perhaps you are thinking right now after all the new beginners learn to create what if OOP OOP
more shown in the project program making medium to large scale.
Maybe for now for those of you who are still novices in programming yet
feels, but if you are from now has a good basic understanding
about OOP then when it came time for you to deepen your OOP
already have preparation.
Therefore, in this tutorial I did not even make you become an expert in
OOP. This tutorial is more indicated that you have a good understanding
about OOP.
Creating Objects
An initial step in OOP is the making of objects where the object itself comes
class. Therefore let us first make the class. To create a class in
in PHP you use the keyword class. In the next example we will
create a class Emailer.
<?php
class Emailer
{}
?>
In the above example we create a class called Emailer. In making
class, first we use the class keyword followed by the name
class. Then end with curly braces. Inside the curly braces we
write down the codes so that these classes work as we want.
As you can see, in the example code above we do not enter codes
anything that Emailer class is not doing any work.
Existing code in the class is divided into two groups, namely property and
method. Property is a storage container in the classroom that can
accommodate the information. The simple property can be referred to as a variable in
in the classroom. While the method is a function in the classroom. Now
Let us give a property and two-class method in Emailer.
Thus, the code would look like:
<?php
class Emailer
{
private $EmailAddress; //property
public function getEmailAddress() //method
{
echo $this->$EmailAddress
}
public function setEmailAddress()
{
$this->$EmailAddress;
}
}
?>
For now ignore the keyword public first, private, and $ this is seen
in the code above. We'll discuss it later. In the above example we
add a property named emailaddress that will be used
to accommodate e-mail address. Then we also added two
method, the first named getEmailAddress which serves to show
content emailaddress property. The second is a working setEmailAddress
to enter an email address to the property emailaddress.
Using Class
Class codes should be in the PHP script that uses these classes.
Often classes are created are stored in different files and then
inserted by using the keywords include when necessary in the script
PHP.
To use an object, you first must create the object of a class.
In this context the Emailer class. Creating an object from a class in
English is called instantiating. To make objects in PHP, we
using the new keyword. Setting the sentence is as follows::
$ObjeckName = new ClassName( );
In the following code we will create an object of class Emailer
<?php
class Emailer
{
private $EmailAddress; //property
public function getEmailAddress() //method
{
$this->$EmailAddress
}
public function setEmailAddress()
{
return $this->$EmailAddress;
}
}
//example make objeck in PHP
$emailerObject = new Emailer();
?>
Using Object
We have created a class and create objects from these classes. Now
let's experiment a little with the objects we have created. In code
just created, we have created a property and two class method
Emailer. To use an existing method on an object it must be
used operator -> which then followed with a method that would
used. Here is an example to use the method:
<?php
class Emailer
{
private $EmailAddress; //property
public function getEmailAddress() //method
{
return $this->$EmailAddress;
}
public function setEmailAddress($emailName)
{
$this->$EmailAddress = $emailName;
}
}
//example make objeck in PHP
$emailerObject = new Emailer();
//menggunakan method
$emailerObject->setEmailAddress(�username@example.com�);
echo $emailerObject->getEmailAddress();
?>
When the code is run it will display text on username@example.com
web browser screen. This is one example of object-oriented programming
simplest. Where you use an object in the PHP script
you.
Public and Private
Perhaps the earlier you have to wonder what the public & private
There Emailer class. Property and the existing method in the class can have
public or private nature (there is still one more that is protected, but will be discussed
the next tutorial).
What is the difference between the two? Private means the method or property in
in a class can only be accessed in the class. While the method or
which are public property means the method or property can be accessed at
within and outside the classroom. In the previous code example, we see property
EmailAddres is private. Now we try to property is accessed from outside
emailerObject object, so the code would look like the following:
<?php
class Emailer
{
private $EmailAddress; //property
public function getEmailAddress() //method
{
return $this->$EmailAddress;
}
public function setEmailAddress($emailName)
{
$this->$EmailAddress = $emailName;
}
}
//example make objeck in PHP
$emailerObject = new Emailer();
//mengakses property dari luar objek
$emailerObject->EmailAddress = "username@example.com";
?>
When the code above is run will display the following error message:
Fatal error: Can not access private property Emailer:: $ emailaddress in
C: \ xampp \ htdocs \ gerry \ practice \ test.php on line 21
Consider changing your emailaddress become public property, then run the code again,
then the error message will not appear.
Encapsulation
Encapsulation or in English known as encapsulation is
mechanism to bind the code with the data so that the code dimanipulasinya
and the data contained within it safe from outside interference. Combining the data and
method in a class is called encapsulation. In the previous code example
you just do encapsulation:
<?php
class Emailer
{
private $EmailAddress; //property
public function getEmailAddress() //method
{
return $this->$EmailAddress;
}
public function setEmailAddress($emailName)
{
$this->$EmailAddress = $emailName;
}
}
//example make objeck in PHP
$emailerObject = new Emailer();
?>
$this
Now we are at the end of this short tutorial. Last discussions
is the operator $ this. In a class, $ this is a special variable
to access an existing property in the class being used. $ this
can not be used outside the classroom. Use $ this format is as follows:
$ this-> namavariabel
encapsulation
In sebelunya sample code, the class has a property emailaddress Emailer. You
emailaddress can access the following property:
$ this-> emailaddress
By using $ this to access the property, you can perform various
operations against the emailaddress as follows:
$ this-> emailaddress = "example@example.com";
Note the dollar sign ($), when you begin using the variable name
$ this-> emailaddress = $ UserEmail;
ArrayEmail [$ this-> emailaddress] = $ UserEmail;
variable with a dollar sign. In the example above $ UserEmail. But when
use $ this-> then the variable or property can not use sign
dollars. Such errors often occur at PHP programmers who are still
beginners. But with consistent training, basic errors like this will
disappear by itself.
Method
Method defines what can be done by the object. This method is made in
in the classroom. Easy method is a function (function) in the classroom or
object. In the previous code example you've created two method of
getEmailAddress and setEmailAddress.
To use the method, just as you must use the property
operator -> that the previous code example when you use the method
getEmailAddress and setEmailAddress, the code you typed is as
follows:
$ emailerObject-> setEmailAddress (username@example.com);
echo $ emailerObject-> getEmailAddress ();
Exercise
1. What is object-oriented programming?
2. Mention the difference between classes and objects!
3. Create a class, then create an object and also create a property
and two method in the class.
4. What is the difference between public and private property or in the method?
5. State the purpose $ this!
Closing
Finally tutorial introduction of this object-oriented programming has been completed.
After you finish studying this tutorial, you should begin to understand:
1. What is object-oriented programming.
2. What is the object and class.
3. Making objects & classes in PHP.
4. How to make the nature of an object.
In the next tutorial I intend to discuss:
1. Inheritance (inheritance).
2. Polimorphisme (polymorphism).
3. Constructor
4. Destructor
5. and much more.
If you have any input should be included in the next tutorial,
please send an email to the author. Hopefully this tutorial useful for you!
0 comment:
Post a Comment