SlideShare a Scribd company logo
© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org
Interface
• Interface is an empty class which contains only the
declaration of methods.
• So any class which implements this interface must
contain the declared functions in it.
• Interface is nothing but a strict ruling, which helps to
extend any class and strictly implement all methods
defined in interface.
• A class can use any interface by using the implements
keyword.
• In interface you can only declare methods, but you
cannot write their body.
• That means the body of all methods must remain blank.
• One of the reasons is it implies strict rules while creating
a class.
© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org
Interface Cont…
<?
//interface.dbdriver.php
interface DBDriver
{
public function connect();
public function execute($sql);
}
?>
• Did you notice that the functions are empty in an interface? Now let's
create our MySQLDriver class, which implements this interface.
<?
//class.mysqldriver.php
include("interface.dbdriver.php");
class MySQLDriver implements DBDriver
{
}
?>
• Now if you execute the code above, it will Fatal error, since we have
not implemented all methods in child class.
© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org
Interface Cont…
<?
include("interface.dbdriver.php");
class MySQLDriver implements DBDriver
{
public function connect()
{
//connect to database
}
public function execute($query)
{
//execute the query and output result
}
}
?>
• Let's rewrite our MySQLDriver class as follows.
© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org
Abstract Classes
• An abstract class is almost the same as interface.
• Except that now the methods can contain body.
• An abstract class must also be "extended", not "implemented".
• If the extended classes have some methods with common
functionalities, then you can define those functions in an abstract
class.
<?
//abstract.reportgenerator.php
abstract class ReportGenerator
{
public function generateReport($resultArray)
{
//write code to process the multidimensional result array and
//generate HTML Report
}
}
?>
© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org
Abstract Classes Cont…
• Please note that we can use the abstract class and implement an
interface.
• Similar to declaring a class as abstract, you can also declare any
method as abstract.
• When a method is declared as abstract, it means that the subclass
must override that method.
<?
include("interface.dbdriver.php");
include("abstract.reportgenerator.php");
class MySQLDriver extends ReportGenerator implements DBDriver
{
public function connect()
{
//connect to database
}
public function execute($query)
{
//execute the query and output result
}
/ / you need not declare or write the generateReport method here you need not declare or write the
generateReport method here
//again as it is extended from the abstract class directly."
}
?>
© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org
Abstract Classes Cont…
• Please note that we can use the abstract class and implement an
interface.
• Similar to declaring a class as abstract, you can also declare any
method as abstract.
• When a method is declared as abstract, it means that the subclass
must override that method.
<?
include("interface.dbdriver.php");
include("abstract.reportgenerator.php");
class MySQLDriver extends ReportGenerator implements DBDriver
{
public function connect()
{
//connect to database
}
public function execute($query)
{
//execute the query and output result
}
/ / you need not declare or write the generateReport method here you need not declare or write the
generateReport method here
//again as it is extended from the abstract class directly."
}
?>

More Related Content

PPT
Oo abap-sap-1206973306636228-5
PPTX
iOS development introduction
PPT
Final keyword
PDF
Abap Objects for BW
PPT
Lecture13 abap on line
PPTX
Object oriented programming in java
PDF
Abap object-oriented-programming-tutorials
PPTX
Features of java - javatportal
Oo abap-sap-1206973306636228-5
iOS development introduction
Final keyword
Abap Objects for BW
Lecture13 abap on line
Object oriented programming in java
Abap object-oriented-programming-tutorials
Features of java - javatportal

What's hot (7)

ODP
Beginners Guide to Object Orientation in PHP
PPTX
Final keyword in java
PPTX
Jsp tag library
PPTX
Javascript Road Trip(es6)
PPTX
Functions in sap hana
PPTX
Learn To Code: Introduction to c
PPTX
Java Chapter 04 - Writing Classes: part 2
Beginners Guide to Object Orientation in PHP
Final keyword in java
Jsp tag library
Javascript Road Trip(es6)
Functions in sap hana
Learn To Code: Introduction to c
Java Chapter 04 - Writing Classes: part 2
Ad

Viewers also liked (20)

PPTX
PPT
PHP mysql Installing my sql 5.1
PPT
PHP mysql Sql
PPT
Web forms and html lecture Number 2
PPT
Cookies in php lecture 2
PPT
Web forms and html lecture Number 5
PPT
Error reporting in php
PPT
Adminstrating Through PHPMyAdmin
PPT
Php Mysql
PPT
Time manipulation lecture 1
PPT
PHP mysql Er diagram
PPT
Cookies in php lecture 1
PPT
PHP mysql Database normalizatin
PPT
Reporting using FPDF
PPT
Sql select
PPT
PHP mysql Mysql joins
PPT
Time manipulation lecture 2
PPT
Oop in php lecture 2
PPT
Form validation server side
PPT
Form validation with built in functions
PHP mysql Installing my sql 5.1
PHP mysql Sql
Web forms and html lecture Number 2
Cookies in php lecture 2
Web forms and html lecture Number 5
Error reporting in php
Adminstrating Through PHPMyAdmin
Php Mysql
Time manipulation lecture 1
PHP mysql Er diagram
Cookies in php lecture 1
PHP mysql Database normalizatin
Reporting using FPDF
Sql select
PHP mysql Mysql joins
Time manipulation lecture 2
Oop in php lecture 2
Form validation server side
Form validation with built in functions
Ad

Similar to Oop in php lecture 2 (20)

PDF
PDF
Test Presentation
PDF
PPTX
OOPS Characteristics (With Examples in PHP)
PDF
Abstract Class and Interface in PHP
PPT
9780538745840 ppt ch10
PPT
Introduction to OOP with PHP
PPT
Abstract class
PPT
Abstract class
PPT
Abstract class
PPT
Abstract class
PPT
Abstract class
PPT
Abstract class
PPT
Abstract class
PPTX
Object oreinted php | OOPs
PPTX
abstract class and interface.Net
PPT
Java interfaces & abstract classes
PDF
Advanced PHP Simplified - Sunshine PHP 2018
PDF
Take the Plunge with OOP from #pnwphp
Test Presentation
OOPS Characteristics (With Examples in PHP)
Abstract Class and Interface in PHP
9780538745840 ppt ch10
Introduction to OOP with PHP
Abstract class
Abstract class
Abstract class
Abstract class
Abstract class
Abstract class
Abstract class
Object oreinted php | OOPs
abstract class and interface.Net
Java interfaces & abstract classes
Advanced PHP Simplified - Sunshine PHP 2018
Take the Plunge with OOP from #pnwphp

More from Mudasir Syed (12)

PPT
Filing system in PHP
PPTX
PHP mysql Introduction database
PPT
PHP mysql Aggregate functions
PPT
Form validation client side
PPT
Javascript lecture 4
PPT
Javascript lecture 3
PPT
Javascript 2
PPT
Java script lecture 1
PPTX
Dom in javascript
PPT
Functions in php
PPT
PHP array 2
PPTX
PHP array 1
Filing system in PHP
PHP mysql Introduction database
PHP mysql Aggregate functions
Form validation client side
Javascript lecture 4
Javascript lecture 3
Javascript 2
Java script lecture 1
Dom in javascript
Functions in php
PHP array 2
PHP array 1

Recently uploaded (20)

PPTX
Cell Structure & Organelles in detailed.
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
PPTX
Open Quiz Monsoon Mind Game Final Set.pptx
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Cardiovascular Pharmacology for pharmacy students.pptx
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
PSYCHOLOGY IN EDUCATION.pdf ( nice pdf ...)
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Basic Mud Logging Guide for educational purpose
PPTX
Open Quiz Monsoon Mind Game Prelims.pptx
PDF
Open folder Downloads.pdf yes yes ges yes
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
Cell Structure & Organelles in detailed.
NOI Hackathon - Summer Edition - GreenThumber.pptx
Open Quiz Monsoon Mind Game Final Set.pptx
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Cardiovascular Pharmacology for pharmacy students.pptx
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PSYCHOLOGY IN EDUCATION.pdf ( nice pdf ...)
STATICS OF THE RIGID BODIES Hibbelers.pdf
Microbial disease of the cardiovascular and lymphatic systems
Abdominal Access Techniques with Prof. Dr. R K Mishra
Anesthesia in Laparoscopic Surgery in India
Basic Mud Logging Guide for educational purpose
Open Quiz Monsoon Mind Game Prelims.pptx
Open folder Downloads.pdf yes yes ges yes
FourierSeries-QuestionsWithAnswers(Part-A).pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Renaissance Architecture: A Journey from Faith to Humanism
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...

Oop in php lecture 2

  • 1. © Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org Interface • Interface is an empty class which contains only the declaration of methods. • So any class which implements this interface must contain the declared functions in it. • Interface is nothing but a strict ruling, which helps to extend any class and strictly implement all methods defined in interface. • A class can use any interface by using the implements keyword. • In interface you can only declare methods, but you cannot write their body. • That means the body of all methods must remain blank. • One of the reasons is it implies strict rules while creating a class.
  • 2. © Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org Interface Cont… <? //interface.dbdriver.php interface DBDriver { public function connect(); public function execute($sql); } ?> • Did you notice that the functions are empty in an interface? Now let's create our MySQLDriver class, which implements this interface. <? //class.mysqldriver.php include("interface.dbdriver.php"); class MySQLDriver implements DBDriver { } ?> • Now if you execute the code above, it will Fatal error, since we have not implemented all methods in child class.
  • 3. © Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org Interface Cont… <? include("interface.dbdriver.php"); class MySQLDriver implements DBDriver { public function connect() { //connect to database } public function execute($query) { //execute the query and output result } } ?> • Let's rewrite our MySQLDriver class as follows.
  • 4. © Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org Abstract Classes • An abstract class is almost the same as interface. • Except that now the methods can contain body. • An abstract class must also be "extended", not "implemented". • If the extended classes have some methods with common functionalities, then you can define those functions in an abstract class. <? //abstract.reportgenerator.php abstract class ReportGenerator { public function generateReport($resultArray) { //write code to process the multidimensional result array and //generate HTML Report } } ?>
  • 5. © Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org Abstract Classes Cont… • Please note that we can use the abstract class and implement an interface. • Similar to declaring a class as abstract, you can also declare any method as abstract. • When a method is declared as abstract, it means that the subclass must override that method. <? include("interface.dbdriver.php"); include("abstract.reportgenerator.php"); class MySQLDriver extends ReportGenerator implements DBDriver { public function connect() { //connect to database } public function execute($query) { //execute the query and output result } / / you need not declare or write the generateReport method here you need not declare or write the generateReport method here //again as it is extended from the abstract class directly." } ?>
  • 6. © Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org Abstract Classes Cont… • Please note that we can use the abstract class and implement an interface. • Similar to declaring a class as abstract, you can also declare any method as abstract. • When a method is declared as abstract, it means that the subclass must override that method. <? include("interface.dbdriver.php"); include("abstract.reportgenerator.php"); class MySQLDriver extends ReportGenerator implements DBDriver { public function connect() { //connect to database } public function execute($query) { //execute the query and output result } / / you need not declare or write the generateReport method here you need not declare or write the generateReport method here //again as it is extended from the abstract class directly." } ?>