OOPS in PHP uses object-oriented programming principles. Everything is grouped into self-contained objects that have properties and methods. A class defines the common attributes and behaviors of objects, acting as a blueprint. Objects are instances of a class and inherit its properties and methods. Key OOP concepts in PHP include classes, objects, inheritance, polymorphism, access specifiers, constructors and destructors.
1. OOPS CONCEPT IN PHP
What is meant by oops?
OOP is a design philosophy. It stands for Object Oriented Programming. Object-Oriented
Programming (OOP) uses a different set of programming languages than old procedural
programming languages (C, Pascal, etc.). Everything in OOP is grouped as self sustainable
"objects".
In order to clearly understand the object orientation, let’s take your “hand” as an
example. The “hand” is a class. Your body has two objects of type hand, named left
hand and right hand. Their main functions are controlled/ managed by a set of
electrical signals sent through your shoulders (through an interface). So the shoulder
is an interface which your body uses to interact with your hands. The hand is a well
architected class. The hand is being re-used to create the left hand and the right
hand by slightly changing the properties of it.
Features of OOPS:
OOP or Object Oriented Programming PHP has so many features like:
Class
Object
Polymorphism
Dynamic Binding...etc.
What is a Class?
A class is simply a representation of a type of object. It is the blueprint/ plan/
template that describe the details of an object. A class is the blueprint from which
the individual objects are created. Class is composed of three things: a name,
attributes, and operations. The class contains the methods and properties
Methods=function
Properties=variable
Example:
Class hand{
function left ($datal){
}
function right ($datar){
}
}
$left hand=new hand(); // left hand object is created for the class hand
$left hand-> left ($parma);
$right hand=new hand();//right hand object is created for the class hand
$left hand-> right ($parma);
Example:
class Student // creation of class
{
}
2. Here $name is the name of the object Student is the class name.
For example Fruit is a class, where apple, orange are the object of this class.
More than one object can be built from the same class at the same time, each one
independent of the others.
The variable $this is a special variable and it refers to the same object ie. itself.
What is an Object?
An object can be considered a "thing" that can perform a set of related activities. The
set of activities that the object performs defines the object's behavior.
For example, the hand can grip something or a Student (object) can give the name
or address.
In pure OOP terms an object is an instance of a class.
Once you defined your class, then you can create as many objects as you like of that
class type. Following is an example of how to create object using new operator.
Example:
<?php
class Books{
/* Member variables */
var $price;
var $title;
/* Member functions */
function setPrice($par){
$this->price = $var;
}
function getPrice(){
echo $this->price ."<br/>";
}
function setTitle($par){
$this->title = $par;
}
function getTitle(){
echo $this->title ." <br/>";
}
}
?>
Example:
$physics = new Books; // object creation …
$maths = new Books;
$chemistry = new Books;
3. Calling Member Functions:
After creating the objects, we will be able to call member functions related
to that object. One member function will be able to process member variable of related
object only.
To get the values from the functions:
Constructor
All objects can have a special built-in method called a 'constructor'. Constructors
allow you to initialize your object's properties (translation: give your properties
values,) when you instantiate (create) an object.
Note: If you create a __construct() function (it is your choice,) 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'.
We can call parent's constructor method using parent::__construct() from the child
constructor.
Example:
$physics->setTitle( "Physics for High School" ); // Function Calling by sending the
values
$chemistry->setTitle( "Advanced Chemistry" );
$maths->setTitle( "Algebra" );
$physics->setPrice( 10 );
$chemistry->setPrice( 15 );
$maths->setPrice( 7 );
Example:
$physics->getTitle(); // Getting the values from the function ..
$chemistry->getTitle();
$maths->getTitle();
$physics->getPrice();
$chemistry->getPrice();
$maths->getPrice();
Example:
<?php
class person {
var $name;// variable or properties declare
function __construct($persons_name) { // Constructor creation for the class person
$this->name = $persons_name;
}
4. Create an object with a constructor
Now that we've created a constructor method, we can provide a value for the
$name property when we create our person objects.
You 'feed' the constructor method by providing a list of arguments (like you do
with a function) after the class name.
This is just a tiny example of how the mechanisms built into OO PHP can save you time and
reduce the amount of code you need to write. Less code means less bugs.
Destructor
Constructors are very useful, as I am sure you will agree, but there is more: PHP
also allows you to define class destructors - a function to be called when an object is
Example:
$stefan = new person("Stefan Mischook");
This saves us from having to call the set_name() method reducing the amount of code.
Constructors are common and are used often in PHP, Java etc.
<?php include("class_lib.php"); ?>
</head>
<body>
<?php
$stefan = new person("Stefan Mischook");
echo "Stefan's full name: ".$stefan->get_name();
?>
</body>
</html>
5. deleted. PHP calls destructors as soon as objects are no longer available, and the
destructor function, __destruct(), takes no parameters.
/*Not so fast, get some coffee (don't forget to mix it with some sugar and milk too) !
Destruction complete. Thank you. And good bye!
*/
Like constructors, destructors are only called once - you need to use
parent::__destruct(). The key difference is that you should call parent::__destruct()
after the local code for the destruction so that you are not destroying variables
before using it, for example:
Access Specifiers
Access Specifiers are the keywords in PHP, which will tell us about the visiblity of
property or method.
Public, Private and Protected are the three Access Specifier Keywords in PHP. Below
is the difference between each of them.
Class members declared public can be accessed everywhere.
Members declared protected can be accessed only within the class itself and by
inherited and parent classes.
Members declared as private may only be accessed by the class that defines the
member.
Default access specifier is Public.
Example:
<?php
class Marko{
public function __construct($x){
echo "Not so fast, get some coffee $x !";
}
public function __destruct() {
echo "Destruction complete. Thank you. And good bye!";
}
}
?>
$someObject = new Marko("(don't forget to mix it with some sugar and
milk too)");
Example:
public function __destruct() {
print "{$this->Name} is no more...n";
parent::__destruct();
}
Example:
<?php
class person {
var $name;
public $height;
protected $social_insurance;
private $pinn_number;
function __construct($persons_name) {
$this->name = $persons_name;
6. Note: If you try to access a private property/variable outside of the class, you will
get this:
'Fatal error: Cannot access private property person::$pinn_numberin ...'
Inheritance
Inheritance is a fundamental capability/construct in OOP where you can use one class,
as the base/basis for another class … or many other classes.
Why do it?
Doing this allows you to efficiently reuse the code found in your base class.
Say, you wanted to create a new 'employee' class … since we can say that 'employee' is a
type/kind of 'person', they will share common properties and methods.
… Making some sense?
In this type of situation, inheritance can make your code lighter … because you are reusing
the same code in two different classes. But unlike 'old-school' PHP:
1. You only have to type the code out once.
2. The actual code being reused, can be reused in many (unlimited) classes but it is
only typed out in one place … conceptually, this is sort-of like PHP includes().
// 'extends' is the keyword that enables inheritance
class employee extends person {
function __construct($employee_name) {
$this->set_name($employee_name);
}
} - See more at: http://www.killerphp.com/tutorials/object-oriented-php/php-
objects-page-4.php#sthash.RK19iCcZ.dpuf