Professional Documents
Culture Documents
Unit 3 Advanced Server SideScripting OOP PHP
Unit 3 Advanced Server SideScripting OOP PHP
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
try {
} finally {