Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

Advanced

Server Side Scripting


By : Laxman Kharel
1. Companies wants OOP not procedural.
2. Better for group work.
3. We can easily reuse code.
4. Better Organized code.
5. Better with medium to larges websites
6. It possible to create full reusable applications with less code and
shorter development time.
7. Good level of Abstraction.
• Class : This is a programmer-defined data type, which includes local functions as well as
local data. You can think of a class as a template for making many instances of the same kind
(or class) of object. Classes are created using class keyword. Eg: class College{ }
• Object : They are real world entities. An individual instance of the data structure defined by
a class. You define a class once and then make many objects that belong to it. Objects are
also known as instance.
• To create php object we have to use a new operator. In the given example c1 is the object of
the Class College. Eg: $c1=new College();

Properties : Classes can have variables within it. Those Class member variables are
called properties. A property is a normal PHP variable which is in any data type (integer,
string, array, object, etc).
Methods: Functions that are inside classes are called methods. Their declaration and
behavior are almost similar to normal functions, except their special uses inside the class.
class Mobile {
public $price;
public $title;
function setPrice($par1){
$this->price = $par1;
}
function getPrice(){
echo "Price:\t".$this->price ."<br/>“;
}
function setName($par2){
$this->title = $par2;
}
function getName(){
echo "Mobile Name:\t".$this->title ."<br/>“; }
}
$Iphone=new Mobile();
$Iphone->setName("Iphone XII MAX pro");
$Iphone->setPrice( 200000 );
$Iphone->getName();
$Iphone->getPrice();
• A class holds the methods and properties that are shared by all of the objects that
are created from it.
• Although the objects share the same code, they can behave differently because
they can have different values assigned to them.
• In order to get a property, we write the object name, and then dash greater than
(->), and then the property name.
• The $this keyword indicates that we use the class's own methods and properties,
and allows us to have access to them within the class's scope.
Eg: $this -> propertyName;
$this -> methodName();
Constructor
• It refers to a special type of function which will be called automatically whenever there is an
object formation from a class.
• A constructor is a public method which is named as __construct.
Destructor
• It refers to a special type of function which will be called automatically whenever an object
is deleted or goes out of scope.
• A destructor is a method which is named as __destruct().
• The __destructor method will only be called when the current PHP script is fully executed by
reaching the last line of it, or by exiting it using exit() or die() functions.
• Destructors are for destroying objects and automatically called at the end of execution.
• It is automatically called at the end of execution.
• Encapsulation is all about wrapping the data and method in a single unit.
• Object-Oriented programming sets the access level for member functions or
variables. It means from where it needs to be accessed.
• There are three kinds of access levels or access specifiers.
• Public :Public functions or variables can only be accessed from anywhere. In all
the above examples, we are able to access the public functions and variables
outside the class and everyone can call it.
• Private : Private functions or variables can only be accessed by the class who
created it or Can only be accessed within the class
• Protected : Protected functions or variables can only be used by the class who
created and it’s child class means all the classes which inherit the property of
parent class having protected method or variable.
• when a class derives from another class, it is called inheritance.
• The derived class is the child or sub class
• The class from which the child is derived is called the parent class or super class .
• The child class inherits all the public and protected properties and methods from
the parent class
• A child class can also be inherited by another class.
• Extend keyword is used for inheritance
class superClass{
//property and methods
}
class subClass superClass{
//property and methods
}
• The child class can have its own properties and methods, it can override the
properties and methods of the parent class. When we override the class’s
properties and methods, we rewrite a method or property that exists in the parent
again in the child, but assign to it a different value or code..
• When we create a function in a derived class with the same signature(in other
words a function has the same name, the same number of arguments and the
same type of arguments)as a function in its parent class is called method
overriding.
• If we don’t want to override parent class member and methods by child class we
can use final keyword in front .
• Program overrid.php
: In the program we override moveSteering() and brake() methods by subclass
motorbike. If we don’t override those methods but call using subclass
motorbike object’s it will display the code of super class methods.
• Unified Modeling Language UML is a technique used to design and document
object oriented systems
• UML produces a number of documents, Here we look at the class diagram

Class Diagram Key


The Upper box contains the class name
The middle box contains the class variables
The lower box contains the class methods
The minus (-) sign means private scope
The plus (+) sign means public scope
The hash (#) sign means protected scope
• Polymorphism is basically derived from the Greek word Poly meaning many and
morphism meaning form which in combines means 'many forms'.
• Suppose a person is a teacher, and he is also studying, so he is a student as well. It
means that a person has more than one roles, means, more than one form. Some
times he is acting as a teacher, whereas he is acting as a student at some points.
• Suppose you have created an interface also classes that will implement this
interface class. These classes will have different functionality and they all will share
this common interface.
• Two types of Polymorphism:
• Compile Time : method overloading and operator overloading- It is not
supported by php .
• Run Time: decision is made at runtime. Eg: method overriding.
• In order to implement the polymorphism principle, we can also choose between
abstract classes and interfaces.
• When a class is defined with a abstract keyword in the front, it is called abstract
class . Following points must be taken care while defining abstract class:-
 An abstract class can have methods and properties.
 An abstract class cannot be instantiated, hence we need to create a child
class which extends it.
 If a class has even a single abstract method then the class should also be
abstract.
 Abstract must contain at least one abstract method, but we should only
declare the method, not the implementation i.e., we provide only name of
the method and argument, while the body part is empty.
Syntax:
abstract class Vehicle{
protected $name;
public function setName($name){
$this->name=$name; }
abstract public function calcMilege(); }
• An interface is actually defined by an interface keyword where all the methods are
abstract.
• A PHP interface defines a contract which a class must fulfill.
• All methods declared in interface should be public
• This cannot have a data member and method with implementation i.e. concrete
method.
• Interface class supports multiple inheritance feature
• If a PHP class is a blueprint for objects, an interface is a blueprint for classes.
• An interface can define method names and arguments, but not the contents of
the methods.
• Any classes implementing an interface must implement all methods defined by
the interface.
Syntax:
Interface Shape {
public function getArea(); }
• Static variables or static functions are directly related with class.
• It can access static variables or functions directly with the class names.
• We Use the ‘static’ keyword to declare a static function or static variable.
• Because static methods are callable without an instance of the object created, the
pseudo-variable $this is not available inside methods declared as static.

When to define static methods ?


The static keyword is used in the context of variables and methods that are
common to all the objects of the class. Therefore, any logic which can be shared
among multiple instances of a class should be extracted and put inside the static
method. PHP static methods are most typically used in classes containing static
methods
Program: Static.php
• Exception is an event that interrupts the normal flow of execution. It is a disruption
during the execution of the program.

Why do we need Exception?


When an Exception occurs the normal flow of the program is disrupted and the
program/Application terminates abnormally, which is not recommended,
therefore, these exceptions are to be handled. Flow of the program is disrupted
and the program/Application terminates abnormally, which is not recommended,
therefore, these exceptions are to be handled.

What is Exception handling?


Exception Handling is a mechanism to handle runtime errors .
PHP provides following specialized keywords for this purpose.
try: The try block contains the code that may potentially throw an exception. All of
the code within the try block is executed until an exception is potentially thrown..
catch: This block of code will be called only if an exception occurs within the try code
block. The code within catch statement must handle the exception that was
thrown.
throw: It is used to signal the occurrence of a PHP exception. It is also used to list the
exceptions that a function throws, but doesn’t handle itself. After it throws
exception The PHP runtime will try to find a catch statement to handle the
exception.
finally: Code within the finally block will always be executed after the try and catch
blocks, regardless of whether an exception has been thrown, and before
normal execution resumes. This is useful for scenarios like closing a database
connection regardless if an exception occurred or not..
Syntax:

try {

// run your code here

} catch (exception $e)

//code to handle the exception

} finally {

//optional code that always runs

You might also like