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)

PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Cell Structure & Organelles in detailed.
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
Lesson notes of climatology university.
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Classroom Observation Tools for Teachers
PDF
Computing-Curriculum for Schools in Ghana
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
Cell Types and Its function , kingdom of life
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Supply Chain Operations Speaking Notes -ICLT Program
Abdominal Access Techniques with Prof. Dr. R K Mishra
Microbial disease of the cardiovascular and lymphatic systems
Cell Structure & Organelles in detailed.
human mycosis Human fungal infections are called human mycosis..pptx
PPH.pptx obstetrics and gynecology in nursing
Lesson notes of climatology university.
TR - Agricultural Crops Production NC III.pdf
O7-L3 Supply Chain Operations - ICLT Program
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Classroom Observation Tools for Teachers
Computing-Curriculum for Schools in Ghana
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Cell Types and Its function , kingdom of life
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
STATICS OF THE RIGID BODIES Hibbelers.pdf
Final Presentation General Medicine 03-08-2024.pptx
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx

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." } ?>