OOP and Exception Handling
OOP and Exception Handling
in PHP
Khalilullah Akbari
+93 729908855
[email protected]
PHP OOP
Definition
OOP stands for Object-Oriented Programming.
Procedural programming is about writing procedures or functions that perform
operations on the data, while object-oriented programming is about creating objects
that contain both data and functions.
Example:
There is a class of Car, and its objects are Volvo, Toyota, Audi, …
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...
}
?>
Example
<?php
class Fruit { // Making objects
// Properties
public $name; $apple = new Fruit();
public $color; $banana = new Fruit();
$apple->set_name('Apple');
// Methods $banana->set_name('Banana');
function set_name($name) {
$this->name = $name; echo $apple->get_name();
} echo "<br>";
function get_name() { echo $banana->get_name();
return $this->name;
} ?>
}
The $this Keyword
The $this keyword refers to the current object, and is only available inside methods.
<?php
Example: class Fruit {
public $name;
function set_name($nameofuser) {
$this->name = $nameofuser;
}
}
$apple = new Fruit();
$apple->set_name("Apple");
echo $apple->name;
?>
Constructor
<?php
class Fruit {
A constructor allows you to initialize an public $name;
object's properties upon creation of the public $color;
object.
function __construct($name) {
$this->name = $name;
If you create a __construct() function, PHP }
will automatically call this function when you function get_name() {
return $this->name;
create an object from a class. }
}
<?php
class Fruit {
public $name;
protected $color;
private $weight;
}
Inheritance
PHP 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.
❑ They allow for better organization by grouping classes that work together to
perform a task
❑ They allow the same name to be used for more than one class.
Declaring a Namespace
Namespaces are declared at the beginning of a file using the namespace keyword.
Syntax
Declare a namespace called Html:
namespace Html;
Syntax:
try {
//code that can throw exceptions
} catch(Exception $e) {
//code that runs when an exception is caught
}
Example
<?php
function divide($dividend, $divisor) {
if($divisor == 0) {
throw new Exception("Division by zero");
}
return $dividend / $divisor;
}
try {
echo divide(5, 0);
} catch(Exception $e) {
echo "Unable to divide.";
}
?>
Methods
Method Description
getMessage() Returns a string describing why the exception was thrown
getPrevious() If this exception was triggered by another one, this method returns the previous
exception. If not, then it returns null
getFile() Returns the full path of the file in which the exception was thrown
getLine() Returns the line number of the line of code which threw the exception
Example
try {
echo divide(5, 0);
} catch(Exception $ex) {
<?php
$code = $ex->getCode();
function divide($dividend, $divisor) {
$message = $ex->getMessage();
if($divisor == 0) {
$file = $ex->getFile();
throw new Exception("Division by
$line = $ex->getLine();
zero", 1);
echo "Exception thrown in $file on line
}
$line: [Code $code]
return $dividend / $divisor;
$message";
}
}
?>
Thank You