UNIT -IV-WT-PHP-IIB.COM(CA)
UNIT -IV-WT-PHP-IIB.COM(CA)
WEB TECHNOLOGY(PHP)
UNIT- IV
Using Functions and Classes: Creating User-Defined Functions - Creating Classes – Using
Advanced OOP Concepts.
Class always start with "class" keyword. After this write class name without parentheses.
Syntax
class demo
code to be executed
Eg
<?php
function add()
$x=800;
$y=200;
$sum=$x+$y;
function sub()
$x=1000;
$y=200;
$sub=$x-$y;
Object
Eg
<?php
class demo
{
function add()
{
$x=800;
$y=200;
$sum=$x+$y;
}
function sub()
{
$x=1000;
$y=500;
$sub=$x-$y;
}
}
$obj->add();
$obj->sub();
?>
Ex ii
<?php
class demo
{
function test()
{
echo "Users";
}
}
echo $obj->msg;
$obj->test();
?>
<?php
class demo
{
function add($a,$b)
{
$sum=$a+$b;
echo "Sum=".$sum."<br/>";
}
function sub($x,$y)
{
$sub=$x-$y;
echo "Subtraction=".$sub;
}
}
$obj->add(800,200);
$obj->sub(1000,500);
?>
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!
<!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
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() {
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;
}
}
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();
Inheritance
<!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() {
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...
}
?>
<!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!