SlideShare a Scribd company logo
1 of 29
PHP
Object Oriented Concepts
PHP 4, PHP 5 & PHP 6
๏‚—There are substantial differences between PHP 4 and PHP 5.
Most of the hype was around the new object model, which
was completely rewritten in PHP5. The PHP 5 version is
much more complete, and performs much better as well. In
PHP 4, objects were really just primitive data types, and
were referenced by value. In an attempt to retain as much
backward compatibility as possible in PHP 5 allows
compatibility with the version 4 methods.
๏‚—With the release of PHP 5 in 2004, PHP programmers
finally had the power to code like the Java and C#, PHP
finally had a complete OOP infrastructure.
๏‚—PHP 6 has more features of object Oriented Concepts.
Step by Step Process
๏‚— The difference between building a PHP application the old fashioned
(procedural) way versus the OOP way.
๏‚— What the basic OOP principles are, and how to use them in PHP?
๏‚— When to use OOP in your PHP scripts?
Object Oriented PHP
How to develop a OO PHP ?? to get into this we are going to divide the
process into 22 steps by which we can get a basic idea to develop an
application in OOP Concepts.
STEP 1
First lets create 2 PHP files
index.php
class_lib.php
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
Create a simple PHP class (in class_lib.php)
Instead of having a bunch of functions, variables and code floating
around, to design our PHP scripts in the OOP way, we need to create
our own classes.
keyword 'class'
STEP 2 ( conti ...)
<?php
class classname
{
}
?>
STEP 3 (add data to your class)
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'.
When you create a variable inside a class, it is called a 'property'.
<?php
class classname
{
// var $name is called as properties of class var keyword
var $name;
}
?>
STEP 4 (add functions/methods to your class)
Functions also referred by different name when created inside a class - they
are called 'methods'.
A class's methods are used to manipulate its own data / properties.
<?php
class mfs_employee
{
var $name;
function set_name($new_name)
{
$this->name = $new_name;
}
function get_name()
{
return $this->name;
}
}
?>
STEP 5 (getter and setter functions)
We've created two interesting functions/methods: get_name() and set_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.
NOTE : Another convention (a naming convention,) is that getter and setter
names should match the property names.
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;
}
}
?>
STEP 6 (The '$this' variable)
$this->name = $new_name;
$this is a built-in variable which points to the current object. Or in other
words, $this is a special self-referencing variable. We use $this to access
properties and to call other methods of the current class.
STEP 7 (Use our class in our main PHP page : index.php )
We should not create the PHP classes in our main page, else it will
break the main purpose of building applications in OOP.
So in index.php include the file ( class_lib.php )
<?php include('class_lib.php'); ?>
STEP 8 ( Instantiate/create your object )
Classes are the blueprints/templates of php objects. Classes don't
actually become objects until you do something called: instantiation.
When you instantiate a class, you create an instance of it ... thus
creating the object.
In other words, instantiation is the process of creating an instance
of an object in memory. What memory? The server's memory of
course!
<?php
$obj_mfsemp = new mfs_employee();
?>
Note: The variable $obj_mfsemp becomes a handle/reference to
our newly created mfs_employee class. It is a 'handle', because we
will use $obj_mfsemp to control and use the mfs_employee class.
STEP 9 ( new keyword )
To create an object out of a class, you need to use the 'new'
keyword.
When creating/instantiating a class, we can optionally add brackets
to the class name, as below example. To be clear, we can see in the
code below how we create multiple objects from the same class.
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 ;
?>
STEP 10 ( Set an objects properties )
Now that we've created/instantiated our two separate
'mfs_employee' objects, we can set their properties using the
methods (the setters) we created.
Please keep in mind that though both our mfs_employee objects
($obj_mfsemp1 and $obj_mfsemp2) are based on the same
'mfs_employee' class, as far as php is concerned, they are totally
different objects.
<?php
$obj_mfsemp1 = new mfs_employee ();
$obj_mfsemp2 = new mfs_employee ;
$obj_mfsemp1->set_name("Abinash Grahacharya");
$obj_mfsemp2->set_name("Amitabh Pattnaik");
?>
STEP 11 ( Accessing an object's data )
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.
When accessing methods and properties of a class, we use the
arrow (->) operator.
<?php
$obj_mfsemp1 = new mfs_employee ();
$obj_mfsemp2 = new mfs_employee ;
//setting values in the object
$obj_mfsemp1->set_name("Abinash Grahacharya");
$obj_mfsemp2->set_name("Amitabh Pattnaik");
//getting each values from the object
echo $obj_mfsemp1 -> get_name();
echo "<br />";
echo $obj_mfsemp2 -> get_name();
?>
In this short period of time, we have covered
Designed a PHP class.
Generate/created a couple of objects based on your class.
Inserted data into your objects.
Retrieved data from your objects.
Lets now focus on PHP OBJECT.
STEP 12 ( Directly accessing properties - don't do it! )
We don't have to use methods to access objects properties; you can
directly get to them using the arrow operator (->) and the name of
the variable.
For example: with the property $name (in object $obj_mfsemp1,) we
can get its' value like :
<?php
echo $obj_mfsemp1->name;
?>
NOTE : Though doable, it is considered bad practice to do it
because it can lead to trouble down the road. We should use getter
methods instead.
STEP 13 ( Constructor )
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;
}
}
?>
STEP 14 ( 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 objects for the
class mfs_employee.
We 'feed' the constructor method by providing a list of arguments
(like we do with a function) after the class name at the time of object
declaration.
Not a constructor
<?php
$obj_mfsemp1 = new mfs_employee ();
?>
When have constructor
<?php
$obj_con_mfsemp3 = new mfs_employee (โ€œAbinash Grahacharyaโ€);
?>
STEP 15 ( access modifiers )
One of the fundamental principles in OOP is 'encapsulation'. The
idea is that we create cleaner better code, if you restrict access to
the data structures (properties) in our objects.
Encapsulation : Storing data/properties and functions/methods in a
single unit (class) is encapsulation. Data cannot be accessible to
the outside world and only those functions which are stored in the
class can access it.
We restrict access to class properties using something called
'access modifiers'. There are 3 access modifiers:
1. public
2. private
3. protected
'Public' is the default modifier.
STEP 15 ( access modifiers ) conti...
<?php
class mfs_employee
{
var $name;
public $designation = 'SW Engineer';
protected $standard_charted_pin = '756472';
private $gps_password = 'mindfire';
function __construct($con_name)
{
$this->name = $con_name;
}
function set_name($new_name)
{
$this->name = $new_name;
}
function get_name()
{
return $this->name;
}
}
//NOTE : when ever we are using var it is treated as public
?>
STEP 16 ( Restricting access to properties )
Properties declared as 'public' have no access restrictions, meaning anyone
can access them.
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;
?>
STEP 17 ( Restricting access to methods )
Like properties, you can control access to methods using one of the three
access modifiers:
1. public
2. protected
3. private
<?php
class mfs_employee
{
var $name;
public $designation = 'SW Engineer';
protected $standard_charted_pin =
'756472';
private $gps_password = 'mindfire';
private function getpin()
{
return $this-
>standard_charted_pin ;
}
}
?>
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'.
STEP 18 ( Inheritance - reusing code the OOP way )
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 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.
1. You only have to type the code out once.
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().
STEP 18 ( Inheritance - reusing code the OOP way ) conti..
// 'extends' is the keyword that enables
inheritance
class sales_people extends mfs_employee
{
function
__construct($employee_name)
{
$this ->
set_name($employee_name);
}
}
<?php
class mfs_employee
{
var $name;
public $designation = 'SW Engineer';
protected $standard_charted_pin
= '756472';
private $gps_password = 'mindfire';
function __construct($con_name)
{
$this->name = $con_name;
}
function set_name($new_name)
{
$this->name = $new_name;
}
function get_name()
{
return $this->name;
}
}
//NOTE : when ever we are using var it is treated as public
?>
STEP 19 ( Inheritance - reusing code the OOP way how to access )
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.
Notice how we are able to use set_name() in 'sales_people', even though we
did not declare that method in the 'sales_people' class. That's because we
already created set_name() in the class 'mfs_employee'.
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.
// 'extends' is the keyword that enables
inheritance
class sales_people extends mfs_employee
{
function
__construct($employee_name)
{
$this ->
set_name($employee_name);
}
}
STEP 20 ( Inheritance - reusing code the OOP way- How to access )
<?php
class mfs_employee
{
var $name;
public $designation = 'SW Engineer';
protected $standard_charted_pin
= '756472';
private $gps_password = 'mindfire';
function __construct($con_name)
{
$this->name = $con_name;
}
function set_name($new_name)
{
$this->name = $new_name;
}
function get_name()
{
return $this->name;
}
}
//NOTE : when ever we are using var it is treated as public
?>
// 'extends' is the keyword that enables inheritance
class sales_people extends mfs_employee
{
function __construct($employee_name)
{
$this ->
set_name($employee_name);
}
}
//In PHP file
<?php
$sp_obj_c2 = new sales_people("class2 names");
echo $sp_obj_c2 -> get_name() ;
?>
This is a classic example of how OOP
can reduce the number of lines of code
(don't have to write the same methods
twice) while still keeping your code
modular and much easier to maintain.
STEP 21 ( Overriding Methods )
Sometimes (when using inheritance,) we may need to change how a
method works from the base class.
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.
We have to 'override' the ''mfs_employee' classes version of set_name(),
by declaring the same method in 'sales_people'.
// 'extends' is the keyword that enables inheritance
class sales_people extends mfs_employee
{
function __construct($employee_name)
{
$this ->
set_name($employee_name);
}
function set_name($new_name)
{
if ($new_name[0] == "S")
{
$this->name =
$new_name;
}
}
}
//In PHP file
<?php
$sp_obj_c2 = new sales_people("class2 names");
echo $sp_obj_c2 -> get_name() ;
$sp_obj_c2 = new sales_people("So Check it");
echo $sp_obj_c2 -> get_name() ;
?>
STEP 22 ( Overriding Methods ) cont..
Sometimes we may need to access our base class's version of a method over
lode in the derived (sometimes called 'child') class.
In our example, we overrode the set_name() method in the 'sales_people' class.
Now We have to used the following code :
mfs_employee::set_name($new_name);
to access the parent class' (mfs_employee) version of the set_name() method
// 'extends' is the keyword that enables inheritance
class sales_people extends mfs_employee
{
function __construct($employee_name)
{
$this ->
set_name($employee_name);
}
function set_name($new_name)
{
if ($new_name == "Stefan
Sucks")
{
$this->name =
$new_name;
}
}
function set_name_old_style($new_name)
{
mfs_employee::set_name($new_name);
}
}
:: will tell to PHP to search for set_name() in
the 'base' class.
STEP 22 ( Overriding Methods ) cont..
Also by using the parent keyword we can call the parent methods if it is overloaded
// 'extends' is the keyword that enables inheritance
class sales_people extends mfs_employee
{
function __construct($employee_name)
{
$this ->
set_name($employee_name);
}
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);
}
}
Our Expertise in PHP
๏‚— We have solid 8+ years of experience in PHP development.
๏‚— Our PHP development team has gained expertise in more
than 100 projects.
๏‚— We have worked on and delivered various applications,
systems and software with PHP across various industries.
Thank you for viewing the slides. Hope it did add value.
For further queries contact us or
call 1-248-686-1424
www.mindfiresolutions.com

More Related Content

What's hot

A Gentle Introduction To Object Oriented Php
A Gentle Introduction To Object Oriented PhpA Gentle Introduction To Object Oriented Php
A Gentle Introduction To Object Oriented PhpMichael Girouard
ย 
oop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.comoop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.comtutorialsruby
ย 
Intermediate OOP in PHP
Intermediate OOP in PHPIntermediate OOP in PHP
Intermediate OOP in PHPDavid Stockton
ย 
Class and Objects in PHP
Class and Objects in PHPClass and Objects in PHP
Class and Objects in PHPRamasubbu .P
ย 
ZendCon2010 The Doctrine Project
ZendCon2010 The Doctrine ProjectZendCon2010 The Doctrine Project
ZendCon2010 The Doctrine ProjectJonathan Wage
ย 
Building a Pluggable Plugin
Building a Pluggable PluginBuilding a Pluggable Plugin
Building a Pluggable PluginBrandon Dove
ย 
iPhone Seminar Part 2
iPhone Seminar Part 2iPhone Seminar Part 2
iPhone Seminar Part 2NAILBITER
ย 
Object Oriented Programming Basics with PHP
Object Oriented Programming Basics with PHPObject Oriented Programming Basics with PHP
Object Oriented Programming Basics with PHPDaniel Kline
ย 
Patterns In-Javascript
Patterns In-JavascriptPatterns In-Javascript
Patterns In-JavascriptMindfire Solutions
ย 
Object Oriented Programming With PHP 5 #2
Object Oriented Programming With PHP 5 #2Object Oriented Programming With PHP 5 #2
Object Oriented Programming With PHP 5 #2Wildan Maulana
ย 
Javascript Design Patterns
Javascript Design PatternsJavascript Design Patterns
Javascript Design PatternsSubramanyan Murali
ย 
Cool Object Building With PHP
Cool Object Building With PHPCool Object Building With PHP
Cool Object Building With PHPwensheng wei
ย 
Task 2
Task 2Task 2
Task 2EdiPHP
ย 
Doctrine with Symfony - SymfonyCon 2019
Doctrine with Symfony - SymfonyCon 2019Doctrine with Symfony - SymfonyCon 2019
Doctrine with Symfony - SymfonyCon 2019julien pauli
ย 
Drupal 8: Entities
Drupal 8: EntitiesDrupal 8: Entities
Drupal 8: Entitiesdrubb
ย 

What's hot (18)

A Gentle Introduction To Object Oriented Php
A Gentle Introduction To Object Oriented PhpA Gentle Introduction To Object Oriented Php
A Gentle Introduction To Object Oriented Php
ย 
oop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.comoop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.com
ย 
Intermediate OOP in PHP
Intermediate OOP in PHPIntermediate OOP in PHP
Intermediate OOP in PHP
ย 
Class and Objects in PHP
Class and Objects in PHPClass and Objects in PHP
Class and Objects in PHP
ย 
ZendCon2010 The Doctrine Project
ZendCon2010 The Doctrine ProjectZendCon2010 The Doctrine Project
ZendCon2010 The Doctrine Project
ย 
Building a Pluggable Plugin
Building a Pluggable PluginBuilding a Pluggable Plugin
Building a Pluggable Plugin
ย 
iPhone Seminar Part 2
iPhone Seminar Part 2iPhone Seminar Part 2
iPhone Seminar Part 2
ย 
Object Oriented Programming Basics with PHP
Object Oriented Programming Basics with PHPObject Oriented Programming Basics with PHP
Object Oriented Programming Basics with PHP
ย 
Oop concepts in python
Oop concepts in pythonOop concepts in python
Oop concepts in python
ย 
Patterns In-Javascript
Patterns In-JavascriptPatterns In-Javascript
Patterns In-Javascript
ย 
Object Oriented Programming With PHP 5 #2
Object Oriented Programming With PHP 5 #2Object Oriented Programming With PHP 5 #2
Object Oriented Programming With PHP 5 #2
ย 
Javascript Design Patterns
Javascript Design PatternsJavascript Design Patterns
Javascript Design Patterns
ย 
Cool Object Building With PHP
Cool Object Building With PHPCool Object Building With PHP
Cool Object Building With PHP
ย 
OOP in PHP
OOP in PHPOOP in PHP
OOP in PHP
ย 
Task 2
Task 2Task 2
Task 2
ย 
Doctrine with Symfony - SymfonyCon 2019
Doctrine with Symfony - SymfonyCon 2019Doctrine with Symfony - SymfonyCon 2019
Doctrine with Symfony - SymfonyCon 2019
ย 
Drupal 8: Entities
Drupal 8: EntitiesDrupal 8: Entities
Drupal 8: Entities
ย 
PHP MVC
PHP MVCPHP MVC
PHP MVC
ย 

Viewers also liked

cPanel User Manual
cPanel User ManualcPanel User Manual
cPanel User Manualwebhostingguy
ย 
ใ„ใกใฐใ‚“็ฐกๅ˜ใชconcrete5ใƒ†ใƒผใƒž
ใ„ใกใฐใ‚“็ฐกๅ˜ใชconcrete5ใƒ†ใƒผใƒžใ„ใกใฐใ‚“็ฐกๅ˜ใชconcrete5ใƒ†ใƒผใƒž
ใ„ใกใฐใ‚“็ฐกๅ˜ใชconcrete5ใƒ†ใƒผใƒžHideki MACHIDA
ย 
Developing Your Ultimate Package
Developing Your Ultimate PackageDeveloping Your Ultimate Package
Developing Your Ultimate PackageSimon Collison
ย 
Technote Index Map Help
Technote Index Map HelpTechnote Index Map Help
Technote Index Map Helpguest0e7fb2
ย 
The Original Hacker nรบmero 11.
The Original Hacker nรบmero 11.The Original Hacker nรบmero 11.
The Original Hacker nรบmero 11.Huehue 1
ย 
La evoluciรณn de los medios de informaciรณn
La evoluciรณn de los medios de informaciรณnLa evoluciรณn de los medios de informaciรณn
La evoluciรณn de los medios de informaciรณnYolanda Santana
ย 
password (facebook)
password (facebook) password (facebook)
password (facebook) Mr. FM
ย 
concrete5 5.7ใงใƒ†ใƒผใƒžไฝœใฃใฆใฟใ‚ˆใ† ่ถ…ๅˆ็ดš็ทจ
concrete5 5.7ใงใƒ†ใƒผใƒžไฝœใฃใฆใฟใ‚ˆใ†่ถ…ๅˆ็ดš็ทจconcrete5 5.7ใงใƒ†ใƒผใƒžไฝœใฃใฆใฟใ‚ˆใ†่ถ…ๅˆ็ดš็ทจ
concrete5 5.7ใงใƒ†ใƒผใƒžไฝœใฃใฆใฟใ‚ˆใ† ่ถ…ๅˆ็ดš็ทจYuriko Kamimori
ย 
Real Developer Tools for WordPress by Stefan Didak
Real Developer Tools for WordPress by Stefan DidakReal Developer Tools for WordPress by Stefan Didak
Real Developer Tools for WordPress by Stefan DidakEast Bay WordPress Meetup
ย 
TYPO3 CMS 7.1 - Die Neuerungen - pluswerk
TYPO3 CMS 7.1 - Die Neuerungen - pluswerkTYPO3 CMS 7.1 - Die Neuerungen - pluswerk
TYPO3 CMS 7.1 - Die Neuerungen - pluswerkdie.agilen GmbH
ย 
A portrait chapter_1
A portrait chapter_1A portrait chapter_1
A portrait chapter_1mellamogaby
ย 
TYPO3 CMS 6.2 LTS - Die Neuerungen
TYPO3 CMS 6.2 LTS - Die NeuerungenTYPO3 CMS 6.2 LTS - Die Neuerungen
TYPO3 CMS 6.2 LTS - Die Neuerungendie.agilen GmbH
ย 
Sql Injection and Entity Frameworks
Sql Injection and Entity FrameworksSql Injection and Entity Frameworks
Sql Injection and Entity FrameworksRich Helton
ย 

Viewers also liked (20)

Livestock and Land
Livestock and LandLivestock and Land
Livestock and Land
ย 
NASL
NASLNASL
NASL
ย 
Clipagem tarumรฃ setembro 2010
Clipagem tarumรฃ   setembro 2010Clipagem tarumรฃ   setembro 2010
Clipagem tarumรฃ setembro 2010
ย 
cPanel User Manual
cPanel User ManualcPanel User Manual
cPanel User Manual
ย 
C lipagem tarumรฃ maio 2010
C lipagem tarumรฃ   maio 2010C lipagem tarumรฃ   maio 2010
C lipagem tarumรฃ maio 2010
ย 
ใ„ใกใฐใ‚“็ฐกๅ˜ใชconcrete5ใƒ†ใƒผใƒž
ใ„ใกใฐใ‚“็ฐกๅ˜ใชconcrete5ใƒ†ใƒผใƒžใ„ใกใฐใ‚“็ฐกๅ˜ใชconcrete5ใƒ†ใƒผใƒž
ใ„ใกใฐใ‚“็ฐกๅ˜ใชconcrete5ใƒ†ใƒผใƒž
ย 
Developing Your Ultimate Package
Developing Your Ultimate PackageDeveloping Your Ultimate Package
Developing Your Ultimate Package
ย 
Technote Index Map Help
Technote Index Map HelpTechnote Index Map Help
Technote Index Map Help
ย 
C lipagem tarumรฃ outubro 2010
C lipagem tarumรฃ   outubro 2010C lipagem tarumรฃ   outubro 2010
C lipagem tarumรฃ outubro 2010
ย 
Xdebug confoo11
Xdebug confoo11Xdebug confoo11
Xdebug confoo11
ย 
The Original Hacker nรบmero 11.
The Original Hacker nรบmero 11.The Original Hacker nรบmero 11.
The Original Hacker nรบmero 11.
ย 
La evoluciรณn de los medios de informaciรณn
La evoluciรณn de los medios de informaciรณnLa evoluciรณn de los medios de informaciรณn
La evoluciรณn de los medios de informaciรณn
ย 
password (facebook)
password (facebook) password (facebook)
password (facebook)
ย 
concrete5 5.7ใงใƒ†ใƒผใƒžไฝœใฃใฆใฟใ‚ˆใ† ่ถ…ๅˆ็ดš็ทจ
concrete5 5.7ใงใƒ†ใƒผใƒžไฝœใฃใฆใฟใ‚ˆใ†่ถ…ๅˆ็ดš็ทจconcrete5 5.7ใงใƒ†ใƒผใƒžไฝœใฃใฆใฟใ‚ˆใ†่ถ…ๅˆ็ดš็ทจ
concrete5 5.7ใงใƒ†ใƒผใƒžไฝœใฃใฆใฟใ‚ˆใ† ่ถ…ๅˆ็ดš็ทจ
ย 
Real Developer Tools for WordPress by Stefan Didak
Real Developer Tools for WordPress by Stefan DidakReal Developer Tools for WordPress by Stefan Didak
Real Developer Tools for WordPress by Stefan Didak
ย 
TYPO3 CMS 7.1 - Die Neuerungen - pluswerk
TYPO3 CMS 7.1 - Die Neuerungen - pluswerkTYPO3 CMS 7.1 - Die Neuerungen - pluswerk
TYPO3 CMS 7.1 - Die Neuerungen - pluswerk
ย 
A portrait chapter_1
A portrait chapter_1A portrait chapter_1
A portrait chapter_1
ย 
Google Dorks
Google DorksGoogle Dorks
Google Dorks
ย 
TYPO3 CMS 6.2 LTS - Die Neuerungen
TYPO3 CMS 6.2 LTS - Die NeuerungenTYPO3 CMS 6.2 LTS - Die Neuerungen
TYPO3 CMS 6.2 LTS - Die Neuerungen
ย 
Sql Injection and Entity Frameworks
Sql Injection and Entity FrameworksSql Injection and Entity Frameworks
Sql Injection and Entity Frameworks
ย 

Similar to Introduction Php

oop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.comoop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.comtutorialsruby
ย 
Oop in php_tutorial_for_killerphp.com
Oop in php_tutorial_for_killerphp.comOop in php_tutorial_for_killerphp.com
Oop in php_tutorial_for_killerphp.comayandoesnotemail
ย 
Oop in php tutorial
Oop in php tutorialOop in php tutorial
Oop in php tutorialGua Syed Al Yahya
ย 
Oop in php_tutorial
Oop in php_tutorialOop in php_tutorial
Oop in php_tutorialGregory Hanis
ย 
Oops concepts in php
Oops concepts in phpOops concepts in php
Oops concepts in phpCPD INDIA
ย 
Oops concept in php
Oops concept in phpOops concept in php
Oops concept in phpselvabalaji k
ย 
OOPS IN PHP.pptx
OOPS IN PHP.pptxOOPS IN PHP.pptx
OOPS IN PHP.pptxrani marri
ย 
Object oreinted php | OOPs
Object oreinted php | OOPsObject oreinted php | OOPs
Object oreinted php | OOPsRavi Bhadauria
ย 
09 Object Oriented Programming in PHP #burningkeyboards
09 Object Oriented Programming in PHP #burningkeyboards09 Object Oriented Programming in PHP #burningkeyboards
09 Object Oriented Programming in PHP #burningkeyboardsDenis Ristic
ย 
Object Oriented PHP - PART-1
Object Oriented PHP - PART-1Object Oriented PHP - PART-1
Object Oriented PHP - PART-1Jalpesh Vasa
ย 
Lecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptxLecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptxShaownRoy1
ย 
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering CollegeObject Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering CollegeDhivyaa C.R
ย 
Lecture9_OOPHP_SPring2023.pptx
Lecture9_OOPHP_SPring2023.pptxLecture9_OOPHP_SPring2023.pptx
Lecture9_OOPHP_SPring2023.pptxShaimaaMohamedGalal
ย 
Introduction to OOP with PHP
Introduction to OOP with PHPIntroduction to OOP with PHP
Introduction to OOP with PHPMichael Peacock
ย 
Only oop
Only oopOnly oop
Only oopanitarooge
ย 
Object_oriented_programming_OOP_with_PHP.pdf
Object_oriented_programming_OOP_with_PHP.pdfObject_oriented_programming_OOP_with_PHP.pdf
Object_oriented_programming_OOP_with_PHP.pdfGammingWorld2
ย 

Similar to Introduction Php (20)

Oops in PHP
Oops in PHPOops in PHP
Oops in PHP
ย 
oop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.comoop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.com
ย 
Oop in php_tutorial_for_killerphp.com
Oop in php_tutorial_for_killerphp.comOop in php_tutorial_for_killerphp.com
Oop in php_tutorial_for_killerphp.com
ย 
Oop in php tutorial
Oop in php tutorialOop in php tutorial
Oop in php tutorial
ย 
Oop in php_tutorial
Oop in php_tutorialOop in php_tutorial
Oop in php_tutorial
ย 
Oops concepts in php
Oops concepts in phpOops concepts in php
Oops concepts in php
ย 
Oops concept in php
Oops concept in phpOops concept in php
Oops concept in php
ย 
OOPS IN PHP.pptx
OOPS IN PHP.pptxOOPS IN PHP.pptx
OOPS IN PHP.pptx
ย 
Object oreinted php | OOPs
Object oreinted php | OOPsObject oreinted php | OOPs
Object oreinted php | OOPs
ย 
09 Object Oriented Programming in PHP #burningkeyboards
09 Object Oriented Programming in PHP #burningkeyboards09 Object Oriented Programming in PHP #burningkeyboards
09 Object Oriented Programming in PHP #burningkeyboards
ย 
Object Oriented PHP - PART-1
Object Oriented PHP - PART-1Object Oriented PHP - PART-1
Object Oriented PHP - PART-1
ย 
Php oop (1)
Php oop (1)Php oop (1)
Php oop (1)
ย 
Lecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptxLecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptx
ย 
UNIT III (8).pptx
UNIT III (8).pptxUNIT III (8).pptx
UNIT III (8).pptx
ย 
UNIT III (8).pptx
UNIT III (8).pptxUNIT III (8).pptx
UNIT III (8).pptx
ย 
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering CollegeObject Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
ย 
Lecture9_OOPHP_SPring2023.pptx
Lecture9_OOPHP_SPring2023.pptxLecture9_OOPHP_SPring2023.pptx
Lecture9_OOPHP_SPring2023.pptx
ย 
Introduction to OOP with PHP
Introduction to OOP with PHPIntroduction to OOP with PHP
Introduction to OOP with PHP
ย 
Only oop
Only oopOnly oop
Only oop
ย 
Object_oriented_programming_OOP_with_PHP.pdf
Object_oriented_programming_OOP_with_PHP.pdfObject_oriented_programming_OOP_with_PHP.pdf
Object_oriented_programming_OOP_with_PHP.pdf
ย 

More from sanjay joshi

Ccna security
Ccna security Ccna security
Ccna security sanjay joshi
ย 
Array in c language
Array in c languageArray in c language
Array in c languagesanjay joshi
ย 
Introduction to c programming language
Introduction to c programming languageIntroduction to c programming language
Introduction to c programming languagesanjay joshi
ย 
Cloud computing
Cloud computingCloud computing
Cloud computingsanjay joshi
ย 
Static and dynamic polymorphism
Static and dynamic polymorphismStatic and dynamic polymorphism
Static and dynamic polymorphismsanjay joshi
ย 
Embeded system
Embeded systemEmbeded system
Embeded systemsanjay joshi
ย 
Distributed database
Distributed databaseDistributed database
Distributed databasesanjay joshi
ย 
Vb and asp.net
Vb and asp.netVb and asp.net
Vb and asp.netsanjay joshi
ย 
Angular js
Angular jsAngular js
Angular jssanjay joshi
ย 
introduction to c programming language
introduction to c programming languageintroduction to c programming language
introduction to c programming languagesanjay joshi
ย 
Cascading Style Sheets
Cascading Style SheetsCascading Style Sheets
Cascading Style Sheetssanjay joshi
ย 
Css3 responsive
Css3 responsive Css3 responsive
Css3 responsive sanjay joshi
ย 
Java script
Java scriptJava script
Java scriptsanjay joshi
ย 
Data Structure And Queue
Data Structure And Queue Data Structure And Queue
Data Structure And Queue sanjay joshi
ย 
Introduction to java
Introduction to javaIntroduction to java
Introduction to javasanjay joshi
ย 
Static and dynamic polymorphism
Static and dynamic polymorphismStatic and dynamic polymorphism
Static and dynamic polymorphismsanjay joshi
ย 
Angularjs
AngularjsAngularjs
Angularjssanjay joshi
ย 
Visual basic
Visual basicVisual basic
Visual basicsanjay joshi
ย 
Distributed database
Distributed databaseDistributed database
Distributed databasesanjay joshi
ย 

More from sanjay joshi (20)

Ccna security
Ccna security Ccna security
Ccna security
ย 
Array in c language
Array in c languageArray in c language
Array in c language
ย 
Introduction to c programming language
Introduction to c programming languageIntroduction to c programming language
Introduction to c programming language
ย 
Cloud computing
Cloud computingCloud computing
Cloud computing
ย 
Static and dynamic polymorphism
Static and dynamic polymorphismStatic and dynamic polymorphism
Static and dynamic polymorphism
ย 
Embeded system
Embeded systemEmbeded system
Embeded system
ย 
Distributed database
Distributed databaseDistributed database
Distributed database
ย 
Vb and asp.net
Vb and asp.netVb and asp.net
Vb and asp.net
ย 
Angular js
Angular jsAngular js
Angular js
ย 
introduction to c programming language
introduction to c programming languageintroduction to c programming language
introduction to c programming language
ย 
Cascading Style Sheets
Cascading Style SheetsCascading Style Sheets
Cascading Style Sheets
ย 
Css3 responsive
Css3 responsive Css3 responsive
Css3 responsive
ย 
Html ppt
Html pptHtml ppt
Html ppt
ย 
Java script
Java scriptJava script
Java script
ย 
Data Structure And Queue
Data Structure And Queue Data Structure And Queue
Data Structure And Queue
ย 
Introduction to java
Introduction to javaIntroduction to java
Introduction to java
ย 
Static and dynamic polymorphism
Static and dynamic polymorphismStatic and dynamic polymorphism
Static and dynamic polymorphism
ย 
Angularjs
AngularjsAngularjs
Angularjs
ย 
Visual basic
Visual basicVisual basic
Visual basic
ย 
Distributed database
Distributed databaseDistributed database
Distributed database
ย 

Recently uploaded

Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
ย 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
ย 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
ย 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
ย 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
ย 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
ย 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxAmita Gupta
ย 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
ย 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
ย 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxdhanalakshmis0310
ย 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
ย 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
ย 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
ย 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
ย 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
ย 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
ย 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
ย 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
ย 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
ย 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
ย 

Recently uploaded (20)

Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
ย 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
ย 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
ย 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
ย 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
ย 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
ย 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
ย 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
ย 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
ย 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
ย 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
ย 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
ย 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
ย 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
ย 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
ย 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
ย 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
ย 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
ย 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
ย 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
ย 

Introduction Php

  • 2. PHP 4, PHP 5 & PHP 6 ๏‚—There are substantial differences between PHP 4 and PHP 5. Most of the hype was around the new object model, which was completely rewritten in PHP5. The PHP 5 version is much more complete, and performs much better as well. In PHP 4, objects were really just primitive data types, and were referenced by value. In an attempt to retain as much backward compatibility as possible in PHP 5 allows compatibility with the version 4 methods. ๏‚—With the release of PHP 5 in 2004, PHP programmers finally had the power to code like the Java and C#, PHP finally had a complete OOP infrastructure. ๏‚—PHP 6 has more features of object Oriented Concepts.
  • 3. Step by Step Process ๏‚— The difference between building a PHP application the old fashioned (procedural) way versus the OOP way. ๏‚— What the basic OOP principles are, and how to use them in PHP? ๏‚— When to use OOP in your PHP scripts?
  • 4. Object Oriented PHP How to develop a OO PHP ?? to get into this we are going to divide the process into 22 steps by which we can get a basic idea to develop an application in OOP Concepts. STEP 1 First lets create 2 PHP files index.php class_lib.php 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 Create a simple PHP class (in class_lib.php) Instead of having a bunch of functions, variables and code floating around, to design our PHP scripts in the OOP way, we need to create our own classes. keyword 'class'
  • 5. STEP 2 ( conti ...) <?php class classname { } ?> STEP 3 (add data to your class) 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'. When you create a variable inside a class, it is called a 'property'. <?php class classname { // var $name is called as properties of class var keyword var $name; } ?>
  • 6. STEP 4 (add functions/methods to your class) Functions also referred by different name when created inside a class - they are called 'methods'. A class's methods are used to manipulate its own data / properties. <?php class mfs_employee { var $name; function set_name($new_name) { $this->name = $new_name; } function get_name() { return $this->name; } } ?>
  • 7. STEP 5 (getter and setter functions) We've created two interesting functions/methods: get_name() and set_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. NOTE : Another convention (a naming convention,) is that getter and setter names should match the property names. 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; } } ?>
  • 8. STEP 6 (The '$this' variable) $this->name = $new_name; $this is a built-in variable which points to the current object. Or in other words, $this is a special self-referencing variable. We use $this to access properties and to call other methods of the current class. STEP 7 (Use our class in our main PHP page : index.php ) We should not create the PHP classes in our main page, else it will break the main purpose of building applications in OOP. So in index.php include the file ( class_lib.php ) <?php include('class_lib.php'); ?>
  • 9. STEP 8 ( Instantiate/create your object ) Classes are the blueprints/templates of php objects. Classes don't actually become objects until you do something called: instantiation. When you instantiate a class, you create an instance of it ... thus creating the object. In other words, instantiation is the process of creating an instance of an object in memory. What memory? The server's memory of course! <?php $obj_mfsemp = new mfs_employee(); ?> Note: The variable $obj_mfsemp becomes a handle/reference to our newly created mfs_employee class. It is a 'handle', because we will use $obj_mfsemp to control and use the mfs_employee class.
  • 10. STEP 9 ( new keyword ) To create an object out of a class, you need to use the 'new' keyword. When creating/instantiating a class, we can optionally add brackets to the class name, as below example. To be clear, we can see in the code below how we create multiple objects from the same class. 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 ; ?>
  • 11. STEP 10 ( Set an objects properties ) Now that we've created/instantiated our two separate 'mfs_employee' objects, we can set their properties using the methods (the setters) we created. Please keep in mind that though both our mfs_employee objects ($obj_mfsemp1 and $obj_mfsemp2) are based on the same 'mfs_employee' class, as far as php is concerned, they are totally different objects. <?php $obj_mfsemp1 = new mfs_employee (); $obj_mfsemp2 = new mfs_employee ; $obj_mfsemp1->set_name("Abinash Grahacharya"); $obj_mfsemp2->set_name("Amitabh Pattnaik"); ?>
  • 12. STEP 11 ( Accessing an object's data ) 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. When accessing methods and properties of a class, we use the arrow (->) operator. <?php $obj_mfsemp1 = new mfs_employee (); $obj_mfsemp2 = new mfs_employee ; //setting values in the object $obj_mfsemp1->set_name("Abinash Grahacharya"); $obj_mfsemp2->set_name("Amitabh Pattnaik"); //getting each values from the object echo $obj_mfsemp1 -> get_name(); echo "<br />"; echo $obj_mfsemp2 -> get_name(); ?>
  • 13. In this short period of time, we have covered Designed a PHP class. Generate/created a couple of objects based on your class. Inserted data into your objects. Retrieved data from your objects. Lets now focus on PHP OBJECT.
  • 14. STEP 12 ( Directly accessing properties - don't do it! ) We don't have to use methods to access objects properties; you can directly get to them using the arrow operator (->) and the name of the variable. For example: with the property $name (in object $obj_mfsemp1,) we can get its' value like : <?php echo $obj_mfsemp1->name; ?> NOTE : Though doable, it is considered bad practice to do it because it can lead to trouble down the road. We should use getter methods instead.
  • 15. STEP 13 ( Constructor ) 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; } } ?>
  • 16. STEP 14 ( 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 objects for the class mfs_employee. We 'feed' the constructor method by providing a list of arguments (like we do with a function) after the class name at the time of object declaration. Not a constructor <?php $obj_mfsemp1 = new mfs_employee (); ?> When have constructor <?php $obj_con_mfsemp3 = new mfs_employee (โ€œAbinash Grahacharyaโ€); ?>
  • 17. STEP 15 ( access modifiers ) One of the fundamental principles in OOP is 'encapsulation'. The idea is that we create cleaner better code, if you restrict access to the data structures (properties) in our objects. Encapsulation : Storing data/properties and functions/methods in a single unit (class) is encapsulation. Data cannot be accessible to the outside world and only those functions which are stored in the class can access it. We restrict access to class properties using something called 'access modifiers'. There are 3 access modifiers: 1. public 2. private 3. protected 'Public' is the default modifier.
  • 18. STEP 15 ( access modifiers ) conti... <?php class mfs_employee { var $name; public $designation = 'SW Engineer'; protected $standard_charted_pin = '756472'; private $gps_password = 'mindfire'; function __construct($con_name) { $this->name = $con_name; } function set_name($new_name) { $this->name = $new_name; } function get_name() { return $this->name; } } //NOTE : when ever we are using var it is treated as public ?>
  • 19. STEP 16 ( Restricting access to properties ) Properties declared as 'public' have no access restrictions, meaning anyone can access them. 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; ?>
  • 20. STEP 17 ( Restricting access to methods ) Like properties, you can control access to methods using one of the three access modifiers: 1. public 2. protected 3. private <?php class mfs_employee { var $name; public $designation = 'SW Engineer'; protected $standard_charted_pin = '756472'; private $gps_password = 'mindfire'; private function getpin() { return $this- >standard_charted_pin ; } } ?> 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'.
  • 21. STEP 18 ( Inheritance - reusing code the OOP way ) 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 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. 1. You only have to type the code out once. 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().
  • 22. STEP 18 ( Inheritance - reusing code the OOP way ) conti.. // 'extends' is the keyword that enables inheritance class sales_people extends mfs_employee { function __construct($employee_name) { $this -> set_name($employee_name); } } <?php class mfs_employee { var $name; public $designation = 'SW Engineer'; protected $standard_charted_pin = '756472'; private $gps_password = 'mindfire'; function __construct($con_name) { $this->name = $con_name; } function set_name($new_name) { $this->name = $new_name; } function get_name() { return $this->name; } } //NOTE : when ever we are using var it is treated as public ?>
  • 23. STEP 19 ( Inheritance - reusing code the OOP way how to access ) 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. Notice how we are able to use set_name() in 'sales_people', even though we did not declare that method in the 'sales_people' class. That's because we already created set_name() in the class 'mfs_employee'. 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. // 'extends' is the keyword that enables inheritance class sales_people extends mfs_employee { function __construct($employee_name) { $this -> set_name($employee_name); } }
  • 24. STEP 20 ( Inheritance - reusing code the OOP way- How to access ) <?php class mfs_employee { var $name; public $designation = 'SW Engineer'; protected $standard_charted_pin = '756472'; private $gps_password = 'mindfire'; function __construct($con_name) { $this->name = $con_name; } function set_name($new_name) { $this->name = $new_name; } function get_name() { return $this->name; } } //NOTE : when ever we are using var it is treated as public ?> // 'extends' is the keyword that enables inheritance class sales_people extends mfs_employee { function __construct($employee_name) { $this -> set_name($employee_name); } } //In PHP file <?php $sp_obj_c2 = new sales_people("class2 names"); echo $sp_obj_c2 -> get_name() ; ?> This is a classic example of how OOP can reduce the number of lines of code (don't have to write the same methods twice) while still keeping your code modular and much easier to maintain.
  • 25. STEP 21 ( Overriding Methods ) Sometimes (when using inheritance,) we may need to change how a method works from the base class. 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. We have to 'override' the ''mfs_employee' classes version of set_name(), by declaring the same method in 'sales_people'. // 'extends' is the keyword that enables inheritance class sales_people extends mfs_employee { function __construct($employee_name) { $this -> set_name($employee_name); } function set_name($new_name) { if ($new_name[0] == "S") { $this->name = $new_name; } } } //In PHP file <?php $sp_obj_c2 = new sales_people("class2 names"); echo $sp_obj_c2 -> get_name() ; $sp_obj_c2 = new sales_people("So Check it"); echo $sp_obj_c2 -> get_name() ; ?>
  • 26. STEP 22 ( Overriding Methods ) cont.. Sometimes we may need to access our base class's version of a method over lode in the derived (sometimes called 'child') class. In our example, we overrode the set_name() method in the 'sales_people' class. Now We have to used the following code : mfs_employee::set_name($new_name); to access the parent class' (mfs_employee) version of the set_name() method // 'extends' is the keyword that enables inheritance class sales_people extends mfs_employee { function __construct($employee_name) { $this -> set_name($employee_name); } function set_name($new_name) { if ($new_name == "Stefan Sucks") { $this->name = $new_name; } } function set_name_old_style($new_name) { mfs_employee::set_name($new_name); } } :: will tell to PHP to search for set_name() in the 'base' class.
  • 27. STEP 22 ( Overriding Methods ) cont.. Also by using the parent keyword we can call the parent methods if it is overloaded // 'extends' is the keyword that enables inheritance class sales_people extends mfs_employee { function __construct($employee_name) { $this -> set_name($employee_name); } 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); } }
  • 28. Our Expertise in PHP ๏‚— We have solid 8+ years of experience in PHP development. ๏‚— Our PHP development team has gained expertise in more than 100 projects. ๏‚— We have worked on and delivered various applications, systems and software with PHP across various industries.
  • 29. Thank you for viewing the slides. Hope it did add value. For further queries contact us or call 1-248-686-1424 www.mindfiresolutions.com