oop in php lecture 2

6
© 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.

Upload: syed-mudasir-shah

Post on 15-Jul-2015

34 views

Category:

Education


2 download

TRANSCRIPT

Page 1: Oop in  php lecture 2

© 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.

Page 2: Oop in  php lecture 2

© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org

Interface Cont…<?//interface.dbdriver.phpinterface 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.phpinclude("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.

Page 3: Oop in  php lecture 2

© 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.

Page 4: Oop in  php lecture 2

© 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.phpabstract class ReportGenerator{ public function generateReport($resultArray) { //write code to process the multidimensional result array and //generate HTML Report }}?>

Page 5: Oop in  php lecture 2

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

Page 6: Oop in  php lecture 2

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