Object Oriented Programming PHP
Object PHP Definition
Real-world objects share two characteristics: They all have state and behavior. Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail). Bicycles also have state (current gear, current pedal cadence, current speed) and behavior (changing gear, changing pedal cadence, applying brakes). Identifying the state and behavior for real-world objects is a great way to begin thinking in terms of object-oriented programming.Adopting object-oriented techniques when developing your PHP scripts and applications can immediately inject greater flexibility and easier maintenance into your development environment. Programs become easier to extend, or debug, and sharing code with other developers on your team (should you have one) becomes simpler.
Objects and Classes
Objects in PHP cannot be declared literally; they must be instantiated from a class.There’s No Substitute for Class
To define a class in PHP, we write the following code:class Bicycle
{
var $attribute1;
var $attribute2;
function method1()
{
// Code here
return $something;
}
function method2()
{
// Code here
}
}
new keyword:
To a new an instance of our class, we need to use the new keyword. To create a new object from the class definition we use the new keyword.$myBicycle = new Bicycle();
$myBicycle2 = new Bicycle();
$myBicycle3 = new Bicycle();
$myBicycle4 = new Bicycle();
Any New Members?
To access the various attributes and methods (members) of our object, we can use the namespace separator->
(sometimes called the arrow operator; although it’s not really an operator). When you are accessing a variable in PHP, the part of code after the $
is known as the namespace.
When you are accessing attributes and methods that are members of an
object, you need to extend that namespace to include the name of your
object. To do this, you reference attributes and methods like so:// Assigning Value to Attributes
$myBicycle->attribute1 = 'Red';
$myBicycle->attribute2 = 'Hero';
// Calling Methods
$returned = $myBicycle->method1();
$myBicycle->method2();
->
separator. Since we can have objects within objects, we can also extend this further. For example, if our $myBicycle
object was a member of another object called $anotherObject
, we could reference the members of $myBicycle
like so:// Assigning Value to Attributes
$anotherObject->myBicycle->attribute1 = 'Red';
$anotherObject->myBicycle->attribute2 = 'Hero';
// Methods
$returned = $anotherObject->myBicycle->method1();
$anotherObject->myBicycle->method2();
$
appears only once before the namespace; this is because a variable name in PHP can only ever have a single dollar sign. If we were to place another $
anywhere in our namespace, we’d be telling PHP we were using variable variables. I don’t really want to go too far off topic here, so have a look at the PHP documentation for more information about variable variables.
The $this
Pseudo-Variable
When writing code within a class definition, you’re never going to
know the name of the object to which it belongs; this is because the
object hasn’t even been instantiated at this point. In order to access
the attributes and methods of your class from within the class itself, we will need to use the $this
pseudo-variable:class Bicycle
{
var $attribute1;
function method1()
{
return $this->attribute1;
}
}
$this
can also be thought of as ‘my own’ or ‘current object’. In our example, we’re returning the value of $attribute1
within the ‘current object’.Inheritance
Object-oriented programming allows classes to inherit commonly used state and behavior from other classes. In this example, Bicycle now becomes the superclass of MountainBike, RoadBike, and TandemBike. Inheritence is the ability of php to extend classes (child classes) that inherit the charactaristics of the parent class.
<?php class RoadBike extendsBicycle {
private $num_spoke; private $handlebars;}
Constructor
Constructors are functions in a class that are automatically called when you create a new instance of a class with new. A function becomes a constructor, when it has the same name as the class. If a class has no constructor, the constructor of the base class will be called, if it exists.The constructor of a class is a special method that is run upon instantiation. It’s main purpose is to set-up the attributes of a class and to establish the class invariant – to basically make sure that the attributes of the class conform to the class interface. A properly written constructor should never contain any functionality that could fail, thus leaving the object in an invalid state.
<?php class A { function A() { echo "I am the constructor of A.<br />\n"; } function B() { echo "I am a regular function named B in class A.<br />\n"; echo "I am not a constructor in A.<br />\n"; } } class B extends A { } // This will call B() as a constructor $b = new B; ?>
Destructor
A destructor function is run when an object is destroyed – its role is to clean up and to free the resources which were used by the object during run-time, and to unlink the object from other objects or resources.<?php
class MyExampleClass {
function __construct() {
print ”In constructor\n”;
$this->name = ”MyExampleClass”;
}
function __destruct() {
print ”Destroying ” . $this->name . ”\n”;
}
}
$myObj = new MyExampleClass ();
?>
PHP 5 introduces a destructor concept similar to that of other object-oriented languages, such as C++. The destructor method will be called as soon as all references to a particular object are removed or when the object is explicitly destroyed or in any order in shutdown
No comments:
Post a Comment