SEMESTER III UNIT I
WEB TECHNOLOGY(PHP)
UNIT- IV
Using Functions and Classes: Creating User-Defined Functions - Creating Classes – Using
Advanced OOP Concepts.
PHP Class Object
Object and Class in PHP
Class is a blueprint of related data
members(proerties/variable) and methods(function).
Classes are the fundamental construct behind oops.
Class is a self contained independent collection of variables and functions, which
work together to perform one or more specific related task.
How to define a class
Class always start with "class" keyword. After this write class name without parentheses.
Syntax
class demo
code to be executed
Eg
<?php
Prof. M.RAJA M.Sc.,M.Phil.,BEd., Page 1
class demo
{
function add()
$x=800;
$y=200;
$sum=$x+$y;
echo "sum of given no=".$sum."<br/>";
function sub()
$x=1000;
$y=200;
$sub=$x-$y;
echo "Sub of given no=".$sub;
Prof. M.RAJA M.Sc.,M.Phil.,BEd., Page 2
?>
Object
Object is the instance of the class.
Once a class has been defined, objects can be created from the class
through "new" keyword.
class methods and properties can directly be accessed through this object instance.
Connector(Dot operator->) symbol used to connect objects to
their properties or methods.
Eg
<?php
class demo
{
function add()
{
$x=800;
$y=200;
$sum=$x+$y;
echo "sum of given no=".$sum."<br/>";
}
function sub()
Prof. M.RAJA M.Sc.,M.Phil.,BEd., Page 3
{
$x=1000;
$y=500;
$sub=$x-$y;
echo "Sub of given no=".$sub;
}
}
$obj= new demo();
$obj->add();
$obj->sub();
?>
Output Sum=1000 Subtraction=500
Ex ii
<?php
class demo
{
Prof. M.RAJA M.Sc.,M.Phil.,BEd., Page 4
var $msg="welcome";
function test()
{
echo "Users";
}
}
$obj= new demo();
echo $obj->msg;
$obj->test();
?>
Output welcome users
Using Parametrized method
<?php
class demo
{
function add($a,$b)
{
$sum=$a+$b;
Prof. M.RAJA M.Sc.,M.Phil.,BEd., Page 5
echo "Sum=".$sum."<br/>";
}
function sub($x,$y)
{
$sub=$x-$y;
echo "Subtraction=".$sub;
}
}
$obj= new demo();
$obj->add(800,200);
$obj->sub(1000,500);
?>
Output Sum=1000 Subtraction=500
PHP User Defined Functions
Besides the built-in PHP functions, it is possible to create your own functions.
A function is a block of statements that can be used repeatedly in a program.
A function will not execute automatically when a page loads.
Prof. M.RAJA M.Sc.,M.Phil.,BEd., Page 6
A function will be executed by a call to the function.
Create a Function
A user-defined function declaration starts with the keyword function, followed by the name
of the function:
Example
function myMessage()
{
echo "Hello world!";
}
Call a Function
To call the function, just write its name followed by parentheses ():
<!DOCTYPE html>
<html>
<body>
<?php
function myMessage() {
echo "Hello world!";
}
myMessage();
?>
</body>
</html>
Output:
Hello world!
n our example, we create a function named myMessage().
The opening curly brace { indicates the beginning of the function code, and the closing curly
brace } indicates the end of the function.
Prof. M.RAJA M.Sc.,M.Phil.,BEd., Page 7
The function outputs "Hello world!".
PHP Function Arguments
Information can be passed to functions through arguments. An argument is just like a
variable.
Arguments are specified after the function name, inside the parentheses. You can add as
many arguments as you want, just separate them with a comma.
The following example has a function with one argument ($fname). When
the familyName() function is called, we also pass along a name, e.g. ("Jani"), and the name is
used inside the function, which outputs several different first names, but an equal last name:
<!DOCTYPE html>
<html>
<body>
<?php
function familyName($fname) {
echo "$fname Refsnes.<br>";
}
familyName("Jani");
familyName("Hege");
familyName("Stale");
familyName("Kai Jim");
familyName("Borge");
?>
</body>
</html>
Output:
Jani Refsnes.
Hege Refsnes.
Stale Refsnes.
Kai Jim Refsnes.
Borge Refsnes.
PHP Default Argument Value
Prof. M.RAJA M.Sc.,M.Phil.,BEd., Page 8
The following example shows how to use a default parameter. If we call the
function setHeight() without arguments it takes the default value as argument:
<!DOCTYPE html>
<html>
<body>
<?php
function setHeight($minheight = 50) {
echo "The height is : $minheight <br>";
}
setHeight(350);
setHeight();
setHeight(135);
setHeight(80);
?>
</body>
</html>
Output:
The height is : 350
The height is : 50
The height is : 135
The height is : 80
Advanced OOPs Concept:
OOP is faster and easier to execute
OOP provides a clear structure for the programs
OOP helps to keep the PHP code DRY "Don't Repeat Yourself", and makes the code
easier to maintain, modify and debug
OOP makes it possible to create full reusable applications with less code and shorter
development time
Classes and Objects
Prof. M.RAJA M.Sc.,M.Phil.,BEd., Page 9
Classes and objects are the two main aspects of object-oriented programming.
Example:
class
Fruit
objects
Apple
Banana
Mango
Define a Class
A class is defined by using the class keyword, followed by the name of the class and a pair of
curly braces ({}). All its properties and methods go inside the braces:
Syntax:
<?php
class Fruit {
// code goes here...
}
?>
we declare a class named Fruit consisting of two properties ($name and $color) and two
methods set_name() and get_name() for setting and getting the $name property:
<?php
class Fruit {
// Properties
public $name;
public $color;
// Methods
function set_name($name) {
$this->name = $name;
}
function get_name() {
Prof. M.RAJA M.Sc.,M.Phil.,BEd., Page 10
return $this->name;
}
}
?>
Define Objects
Classes are nothing without objects! We can create multiple objects from a class. Each object
has all the properties and methods defined in the class, but they will have different property
values.
Objects of a class are created using the new keyword.
In the example below, $apple and $banana are instances of the class Fruit:
<!DOCTYPE html>
<html>
<body>
<?php
class Fruit {
// Properties
public $name;
public $color;
// Methods
function set_name($name) {
$this->name = $name;
}
function get_name() {
return $this->name;
}
}
$apple = new Fruit();
$banana = new Fruit();
$apple->set_name('Apple');
$banana->set_name('Banana');
echo $apple->get_name();
Prof. M.RAJA M.Sc.,M.Phil.,BEd., Page 11
echo "<br>";
echo $banana->get_name();
?>
</body>
</html>
Output:
Apple
Banana
PHP - The $this Keyword
The $this keyword refers to the current object, and is only available inside methods.
Look at the following example:
<?php
class Fruit {
public $name;
}
$apple = new Fruit();
?>
So, where can we change the value of the $name property? There are two ways:
1. Inside the class (by adding a set_name() method and use $this):
Example
<!DOCTYPE html>
<html>
<body>
<?php
class Fruit {
public $name;
function set_name($name) {
$this->name = $name;
}
}
$apple = new Fruit();
Prof. M.RAJA M.Sc.,M.Phil.,BEd., Page 12
$apple->set_name("Apple");
echo $apple->name;
?>
</body>
</html>
Output:
Apple
Inheritance
Inheritance in OOP = When a class derives from another class.
The child class will inherit all the public and protected properties and methods from the
parent class. In addition, it can have its own properties and methods.
An inherited class is defined by using the extends keyword.
Let's look at an example:
<!DOCTYPE html>
<html>
<body>
<?php
class Fruit {
public $name;
public $color;
public function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
public function intro() {
echo "The fruit is {$this->name} and the color is {$this->color}.";
}
}
// Strawberry is inherited from Fruit
class Strawberry extends Fruit {
public function message() {
Prof. M.RAJA M.Sc.,M.Phil.,BEd., Page 13
echo "Am I a fruit or a berry? ";
}
}
$strawberry = new Strawberry("Strawberry", "red");
$strawberry->message();
$strawberry->intro();
?>
</body>
</html>
Output:
Am I a fruit or a berry? The fruit is Strawberry and the color is red.
Traits
PHP only supports single inheritance: a child class can inherit only from one single parent.
So, what if a class needs to inherit multiple behaviors? OOP traits solve this problem.
Traits are used to declare methods that can be used in multiple classes. Traits can have
methods and abstract methods that can be used in multiple classes, and the methods can have
any access modifier (public, private, or protected).
Traits are declared with the trait keyword:
Syntax
<?php
trait TraitName {
// some code...
}
?>
To use a trait in a class, use the use keyword:
Syntax
<?php
class MyClass {
use TraitName;
}
?>
Prof. M.RAJA M.Sc.,M.Phil.,BEd., Page 14
Example
<!DOCTYPE html>
<html>
<body>
<?php
trait message1 {
public function msg1() {
echo "OOP is fun! ";
}
}
class Welcome {
use message1;
}
$obj = new Welcome();
$obj->msg1();
?>
</body>
</html>
Output:
OOP is fun!
Prof. M.RAJA M.Sc.,M.Phil.,BEd., Page 15