1. Classes
are the blueprints of 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’.
2. Class
is a programmer-defined data type, which includes local methods and local
variables.
3. Class
is a collection of objects. Object has properties and behavior.
Defining PHP Classes
The general form for defining a new class in PHP is as follows −
Here is the
description of each line −
· The special form class, followed by the name of the
class that you want to define.
· A set of braces enclosing any number of variable declarations and
function definitions.
· Variable declarations start with the special form var, which is followed by a
conventional $variable name; they
may also have an initial assignment to a constant value.
· Function definitions look much like standalone PHP functions but
are local to the class and will be used to set and access object data.
Example
Here is an example which defines a class of Books type −
<?php
class Books
{
/*
Member variables */
var
$price;
var
$title;
/*
Member functions */
function
setPrice($par){
$this->price = $par;
}
function
getPrice(){
echo
$this->price ."<br/>";
}
function
setTitle($par){
$this->title = $par;
}
function
getTitle(){
echo
$this->title ." <br/>";
}
}
?>
The variable $this is a special variable and it
refers to the same object ie. itself.
Creating Objects in PHP
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 an object
using new operator.
$physics = new Books; $maths = new Books; $chemistry = new Books;
Here we have
created three objects and these objects are independent of each other and they
will have their existence separately. Next, we will see how to access member
function and process member variables.
Calling Member Functions
After creating your objects, you will be able to call member
functions related to that object. One member function will be able to process
member variables of a related objects only.
Following example shows how to set title and prices for the three
books by calling member functions.
$physics->setTitle(
"Physics for High School" );
$chemistry->setTitle(
"Advanced Chemistry" );
$maths->setTitle(
"Algebra" );
$physics->setPrice(
10 );
$chemistry->setPrice(
15 );
$maths->setPrice(
7 );
Now you
call another member functions to get the values set by in the above example –
$physics->getTitle();
$chemistry->getTitle();
$maths->getTitle();
$physics->getPrice();
$chemistry->getPrice();
$maths->getPrice();
This
will produce the following result –
Physics for High School Advanced Chemistry Algebra 10 15 7
Constructor Functions
Constructor Function is a special type of function that is called automatically whenever an object is created. So we take full advantage
of this behavior, by initializing many things through constructor functions.
PHP provides a special function called __construct() (double Underscore) to define a
constructor. You can pass as many as arguments you like into the constructor
function.
Following example will create one constructor for Books class and
it will initialize price and title for the book at the time of object creation.
function __construct( $par1, $par2 ) { $this->title = $par1; $this->price = $par2; }
Now we don't need to call set
function separately to set price and title. We can initialize this two-member variable at the time of object creation only. Check the following example below −
$physics = new
Books( "Physics for High School", 10 );
$maths = new
Books ( "Advanced Chemistry", 15 );
$chemistry =
new Books ("Algebra", 7 );
/* Get those
set values */
$physics->getTitle();
$chemistry->getTitle();
$maths->getTitle();
$physics->getPrice();
$chemistry->getPrice();
$maths->getPrice();
This
will produce the following result –
Physics for High School
Advanced Chemistry Algebra 10 15 7
$this Keyword
If you are following this tutorial from the
beginning or you started from the OOPS concepts, you must have noticed the
usage of $this in some of the
code snippets.
this keyword is used
inside a class, generally withing the member functions to access non-static
members of a class(variables or functions) for the current object.
Let's take an example to understand the usage
of $this.
<?php
class Person {
// first name of person
private $name;
// public function to set value for
name (setter method)
public function setName($name) {
$this->name = $name;
}
// public function to get value of name
(getter method)
public function getName() {
return $this->name;
}
}
// creating class object
$john = new Person();
// calling the public function to set fname
$john->setName("John Wick");
// getting the value of the name variable
echo "My name is " .
$john->getName();
?>
My name is John Wick
In the program above, we have created a private variable in the
class with name $name and we have two public methods setName() and getName() to assign a new value to $name variable and to
get its value respectively.
Whenever we want to call any variable of the class
from inside a member function, we use $this to point to the current object which holds the variable.
We can also use $this to call one a member function of a class inside another member function.
NOTE: If there is
any static member function
or variable in the class, we cannot refer it using the $this.