PHP Object Oriented Concepts
PHP Object Oriented Concepts
PHP Object Oriented Concepts
OOP is all about creating modular code, so our object oriented PHP
code will be contained in dedicated files that we will then insert into our
normal PHP page using PHP 'includes'.
In this case, all our OO PHP code will be in the PHP file: class_lib.php
In OOP codes revolves around a 'class', Classes are the templates that
are used to define objects.
STEP 2
<?php
class classname
{
}
?>
Classes are the blueprints for php objects. One of the big differences
between functions and classes is that a class contains both data (variables)
and functions that form a package called an: 'object'.
<?php
class classname
{
// var $name is called as properties of class var keyword
var $name;
}
?>
Functions also referred by different name when created inside a class - they
are called 'methods'.
<?php
class mfs_employee
{
var $name;
function set_name($new_name)
{
$this->name = $new_name;
}
function get_name()
{
return $this->name;
}
}
?>
These methods follow a common OOP convention that you see in many
languages (including Java and Ruby) - where you create methods to 'set' and
'get' properties in a class.
This way, when other PHP programmers want to use your objects, they will
know that if you have a method/function called 'set_name()', there will be a
property/variable called 'name'.
<?php
class mfs_employee
{
var $name;
function set_name($new_name)
{
$this->name =
$new_name;
}
function get_name()
{
return $this->name;
}
}
?>
$this->name = $new_name;
We should not create the PHP classes in our main page, else it will
break the main purpose of building applications in OOP.
<?php
$obj_mfsemp = new mfs_employee();
?>
From the PHP's engine point of view, each object is its own entity.
<?php
$obj_mfsemp1 = new mfs_employee ();
$obj_mfsemp2 = new mfs_employee ;
?>
<?php
$obj_mfsemp1 = new mfs_employee ();
$obj_mfsemp2 = new mfs_employee ;
$obj_mfsemp1->set_name("Abinash Grahacharya");
$obj_mfsemp2->set_name("Amitabh Pattnaik");
?>
Now we use the getter methods to access the data held in our
objects … this is the same data we inserted into our objects
using the setter methods.
<?php
$obj_mfsemp1 = new mfs_employee ();
$obj_mfsemp2 = new mfs_employee ;
<?php
echo $obj_mfsemp1->name;
?>
All objects can have a special built-in method called a 'constructor'. Constructors
allow you to initialize your object's properties (give values to properties) when we
instantiate (create) an object.
Note: If you create a __construct() function PHP will automatically call the
__construct() method/function when you create an object from your class.
The 'construct' method starts with two underscores (__) and the word 'construct'.
<?php
class mfs_employee
{
var $name;
function __construct($con_name)
{
$this->name =
$con_name;
}
function set_name($new_name)
{
$this->name =
$new_name;
}
function get_name()
{
return $this->name;
}
}
?>
Not a constructor
<?php
$obj_mfsemp1 = new mfs_employee ();
?>
<?php
$obj_con_mfsemp3 = new mfs_employee (“Abinash Grahacharya”);
?>
1. public
2. private
3. protected
<?php
class mfs_employee
{
var $name;
function __construct($con_name)
{
$this->name = $con_name;
}
function set_name($new_name)
{
$this->name = $new_name;
}
function get_name()
{
return $this->name;
}
}
When you declare a property as 'private', only the same class can access the
property.
When a property is declared 'protected', only the same class and classes
derived from that class can access the property - this has to do with inheritance
<?php
$obj_mfsemp1 = new mfs_employee (“Mindfire”);
echo $obj_mfsemp1-> get_name();
//when we try to access private or public properties outside class will through Fatal
Error
echo $obj_mfsemp1-> standard_charted_pin;
?>
Like properties, you can control access to methods using one of the three
access modifiers:
1. public <?php
class mfs_employee
2. protected {
3. private var $name;
protected $standard_charted_pin =
'756472';
private $gps_password = 'mindfire';
?>
Since the method getpin() is 'private', the only place you can use this method is in
the same class - typically in another method in class. If we wanted to call/use this
method directly in our PHP pages, we need to declare it as 'public'.
Why do it?
Doing this allows help to efficiently reuse the code found in our base class.
Say, you wanted to create a new 'sales_people' class … since we can say
that 'mfs_employee' is a type/kind of 'peoples', they will share common
properties and methods.
In this type of situation, inheritance can make our code lighter … because
we are reusing the same code in two different classes.
2. The actual code being reused, can be reused in many classes but it is
only typed out in one place … conceptually, this is sort-of like PHP
includes().
function __construct($con_name) }
{ }
$this->name = $con_name;
}
function set_name($new_name)
{
$this->name = $new_name;
}
function get_name()
{
return $this->name;
}
}
Because the class 'sales_people' is based on the class ' mfs_employee ',
'sales_people' automatically has all the public and protected, properties
and methods of 'mfs_employee' class.
// 'extends' is the keyword that enables
inheritance
}
}
Note: the 'sales_people' class is called children the 'base' class or the
'mfs_employee' class because it's the class that the 'sales_people' is based
on. This class hierarchy can become important down the road when our
projects become more complex.
For example, let's say set_name() method in the 'sales_people' class, have
to do something different than what it does in the 'mfs_employee' class.
Sometimes we may need to access our base class's version of a method over
lode in the derived (sometimes called 'child') class.
}
function set_name($new_name)
{
if ($new_name == "Stefan
Sucks")
:: will tell to PHP to search for set_name() in
{ the 'base' class.
$this->name =
$new_name;
}
}
Also by using the parent keyword we can call the parent methods if it is overloaded
// 'extends' is the keyword that enables inheritance
}
function set_name($new_name)
{
if ($new_name == "Stefan Sucks")
{
$this->name =
$new_name;
}
}
function set_name_old_style($new_name)
{
parent::set_name($new_name);
}