Table of Contents
Introduction to PHP | How to save PHP file | How to write PHP code
PHP Variable & Data Types
PHP Operators | Arithmetic Operators
PHP Operators | Increment | Decrement | Logical | String | Conditional
PHP Operators | Assignment & Comparison (Relational)
PHP Conditional & Looping structure | If statement | If ..else | If..elseif..else | Switch Case | While Loop
PHP Looping Structure | Do..while | For and Form Submission method – GET
GET & POST (form submission method)
Form Submission Method – GET/POST with tabular format
PHP UDF – Argument Function and Default argument function
PHP Variable as function, Return function & Variable Length Argument Functions
PHP array | Create an array | Add and Update elements
Unset and reset values of array and Foreach loop
PHP Built In Functions
Variable Functions & String Functions Part – 1
String Functions Part – 2
String Functions Part – 3
String Functions Part – 4
Array Functions Part 1
Array Functions – Part 2
Date Functions Part – 1
Date Functions Part – 2
Math Functions Part – 1
Math Functions Part – 2
File Handling Functions Part – 1
File Handling Functions Part – 2
File Handling Functions Part – 3
File Handling Functions Part – 4
Miscellaneous Functions
PHP GD Library & its functions
Part 1
Part 2
PHP OOP (Object Oriented Programming)
What is OOP?
OOP is an abbreviation for Object-Oriented Programming.
Writing procedures or functions that perform operations on data is what procedural programming is all about, whereas object-oriented programming is all about creating objects that contain both data and functions.
There are several advantages to object-oriented programming over procedural programming:
- OOP is faster and easier to implement.
- OOP gives the programs a clear structure.
- OOP enables you to keep PHP code DRY (Don’t Repeat Yourself) and enables it to maintain, modify, and debug. OOP allows you to create fully reusable applications with less code and in less time.
Class & Object
The two main components of object-oriented programming are classes and objects.
Class is a blueprint for objects. Sometimes it is also known as template for the objects. Object is an instance of a class. Let us understand these two with example.
Let say, Keyboard is a Class. A Keyboard can have properties like brandname, color, price, number of keys, type. We can define variables like $brandname, $color, $price to hold the values of these properties. Logitech, TVS, Zebronics are its objects.
When an individual object (Logitech, TVS, Zebronics) is created, it inherits all of the class’s behaviours and properties, but each object will have a unique set of values for each property.
Another example, say Subject is a Class and PHP, COA, DS are its objects.
An object is an instance of a class, and a class is a template for objects. Each individual object that is generated inherits the class’s behaviors and properties, although the values of the properties vary from object to object.
How to define a class
The class keyword is used to define a class, which is then followed by the class name and two curly braces {}. All of its attributes (properties) and operations (functions) go inside the braces.
Syntax :
<?php
class classname {
// variable declaration
// function declaration
}
?>
Example : Below we are taking Keyboard as Classname, which consists two properties: brandname and price and four methods set_brandname(), set_price(), get_brandname(), get_price() to get and set the values of brandname($brandname) and price($price). In a class, variables are called properties and functions are called methods.
<?php
class Keyboard
{
private $brandname;
private $price;
//method to set the value of brandname
function set_brandname($brandname)
{
$this->brandname=$brandname;
}
function get_brandname()
{
return ($this->brandname);
}
function set_price($price)
{
$this->price=$price;
}
function get_price()
{
return ($this->price);
}
}
How to define objects
Without objects, classes are useless. A class can be used to create several objects. All the class-defined attributes and methods are present in every object, but their values will vary. The new keyword is used to create objects that belong to a class.
For above class Keyborad, we are going to define objects as follows
<?php
class Keyboard
{
private $brandname;
private $price;
//method to set the value of brandname
function set_brandname($brandname)
{
$this->brandname=$brandname;
}
function get_brandname()
{
return ($this->brandname);
}
function set_price($price)
{
$this->price=$price;
}
function get_price()
{
return ($this->price);
}
}
//object 1
$logitech=new Keyboard();
$logitech->set_brandname("LOGITECH");
echo "Brandname is ".$logitech->get_brandname()."<br>";
$logitech->set_price(1500);
echo "Price is ".$logitech->get_price()."<br>";
//object 2
$tvs=new Keyboard();
$tvs->set_brandname('TVS');
echo "Brandname is ".$tvs->get_brandname()."<br>";
$tvs->set_price(2500);
echo "Price is ".$tvs->get_price()."<br>";
//Output
Logitech
1500
TVS
2500
Access Modifiers
In above example, I have used private as access modifier. Let us discuss it.
Properties and methods can have access modifiers which control where they can be accessed. There are three access modifiers:
- public – The property or method can be accessed from everywhere. This is default
- protected – The property or method can be accessed within the class and by classes derived from that class
- private – The property or method can only be accessed within the class
These are also called Visibility of a class members. Let us take an example to understand this visibility.
Example
<?php
class Student
{
public $nm="Swati";
private $age=18;
protected $marks=90;
}
$s1=new Student();
echo "Name = ".$s1->nm."<br>";
echo "Age = ".$s1->age."<br>";
echo "Marks = ".$s1->marks."<br>";
?>
Output
Name = Swati
Fatal error: Uncaught Error: Cannot access private property Student::$age
Fatal error: Uncaught Error: Cannot access private property Student::$marks
In above example, $nm is accessed outside the class as it is public property, whereas $age can not be accessed outside the class as $age is a private property which can only be accessed or used inside the class and $marks is a protected property so it can be used in class itself and in derived class.
If we use these private and protected properties in the class, it will work. Let us check it in below example.
Example
<?php
class Student
{
public $nm="Swati";
private $age=18;
protected $marks=90;
function display()
{
echo "Name = ".$this->nm."<br>";
echo "Age = ".$this->age."<br>";
echo "Marks = ".$this->marks."<br>";
}
}
$s1=new Student();
$s1->display();
?>
Output
Name = Swati
Age = 18
Marks = 90
$this Keyword
The $this keyword refers to the current object, and is only available inside methods.
Constructor function
Constructor function is a special function that are called automatically when an object is created. In PHP, __construct() (two underscores) is used to define a constructor. Let us take an above example modified using constructor function.
Example :
<?php
class Keyboard
{
private $brandname;
private $price;
//constructor
function __construct($brandname,$price)
{
$this->brandname = $brandname;
$this->price = $price;
}
function get_brandname()
{
return ($this->brandname);
}
function get_price()
{
return ($this->price);
}
}
//object 1
$logitech=new Keyboard("Logitech",1000);
echo "Brandname is ".$logitech->get_brandname()."<br>";
echo "Price is ".$logitech->get_price()."<hr>";
//object 2
$tvs=new Keyboard("TVS",2000);
echo "Brandname is ".$tvs->get_brandname()."<br>";
echo "Price is ".$tvs->get_price()."<br>";
?>
The example above demonstrates how calling the set_brandname() and set_price() methods are avoided when using a function __construct, which minimizes the amount of code. That means now we don’t need to call set functions separately to set brandname and price.
We have created a constructor for Keyboard class with two member variables ($brandname, $price) and it is initialized brandname and price at the time of object creation only.
Destructor function
Destructor function is a function that calls automatically when an object of the class is destroyed or a script terminates. Destructors are like constructors, except that they are called when the object is deleted. __destruct() (two underscores) is used to define a destructor.
Example : Here destructor function is called automatically when script ends.
<?php
class Demo
{
//constructor
function __construct() {
echo "Hello, I am a ";
}
//destructor
function __destruct() {
echo "Destructor ";
}
}
$obj = new Demo();
?>
Output
Hello, I am a Destructor
Inheritance
Inheritance is the concept of accessing one class from another class. Inheritance is the idea that a class can extend the functionality of another class. The new class will contain all the methods and properties of the class it extends, plus any others it lists within its body. We may also override methods and properties from the extended class. We can extend a class using the extends keyword.
If a class extends another class, the properties and methods of all ancestor classes are available in the child class, despite not being declared explicitly. If you wish to access an inherited property, simply refer to it as you would any other local property.
class Father
{
protected $lname;
function set_lname($lname)
{
$this->lname=$lname;
}
}
class Child extends Father
{
private $fname;
function set_fname($fname)
{
$this->fname=$fname;
}
function disp_fullname()
{
echo "Hey, This is ".$this->fname." ".$this->lname;
}
}
$c1=new Child();
$c1->set_fname("Swati");
$c1->set_lname("Lathia");
$c1->disp_fullname();
Scope Resolution Operator ( : : )
The Scope Resolution Operator : : (double colon) is a token that allows access to static, constant and overridden properties or methods of a class.
Example :
<?php
/*Scope Resolution Operator for Class(Constant) and Objects(Static)*/
class my_connection
{
const USERNAME="root";
const PASSWORD="admin";
const DB_NAME ="db_demo";
}
echo my_connection::USERNAME."<br>";
echo my_connection::PASSWORD."<br>";
echo my_connection::DB_NAME."<hr>";
class test_static
{
static $name="H J DOSHI";
}
$obj1=new test_static();
echo "Name of the College is : ".$obj1::$name."<br>";
$obj1::$name="HJD";
echo "Name of the College is : ".$obj1::$name."<br>";
?>
Autoloading Class
Example : In following example, we have created objects of class a, b and c which are automatically load with spl_autoload_register() and can be accessed their disp() methods. This is auto.php file.
<?php
//Autoloading class
spl_autoload_register(function ($myclass)
{
include $myclass.".php";
});
$o1 = new a();//calls a class
$o1->disp();
$o2 = new b();//calls b class
$o2->disp();
$o3 = new c();//calls c class
$o3->disp();
?>
//a.php
<?php
class a
{
function disp()
{
echo "a.disp() is called";
}
}
?>
//b.php
<?php
class b
{
function disp()
{
echo "b.disp() is called";
}
}
?>
//c.php
<?php
class c
{
function disp()
{
echo "c.disp() is called";
}
}
?>