SlideShare uma empresa Scribd logo
1 de 2
Baixar para ler offline
www.vineetsaini.wordpress.com
Abstract Class & Interface in PHP
Abstract Class
Abstract classes are those classes which can not be directly initialized. Or in other word we
can say that you can’t create object of abstract classes.
Abstract classes always created for inheritance purpose. You can only inherit abstract class
in your child class. Many peoples say that in abstract class at least your one method should
be abstract. Abstract method are the method which is only defined but declared. But your
any class has at least one method abstract than your class is abstract class.
Abstract class are also known as base class. We call it base class because abstract class are
not the class which is available directly for creating object. It can only act as parent class of
any normal class. You can use abstract class in class hierarchy. It means one abstract class
can inherit another abstract class also.
We can create abstract classes in php using abstract keyword. Once we will make any class
abstract in php we can’t create object of that class.
1. An abstract method contains no body.
2. An abstract method is not implemented by the base class.
3. There can be no objects of an abstract class.
4. An abstract class can inherit from a class and one or more interfaces.
5. An abstract class can implement code with non-Abstract methods.
6. An abstract class can have constructors or destructor's.
7. An abstract class cannot support multiple inheritance.
<?php
abstract class testParent
{
public function abc()
{
echo 'Vineet';
}
}
class testChild extends testParent
{
public function xyz()
{
echo 'Saini';
}
}
$a = new testChild();
echo $a->abc();
echo $a->xyz();
?>
www.vineetsaini.wordpress.com
Interface
We know that PHP does not support the multiple inheritance. For support multiple
inheritance in php we use Interface.
Interface is just like a class using interface keyword and contains only function declarations
(function with no body). Function is defined in the classes where you call it.
Interface gets useful when you need to extend a class from more than one parent class
since class can’t do this.
An interface cannot contain any functionality. It only contains definitions of the methods
<?php
interface template1
{
public function f1();
}
class test implements template1
{
public function f1()
{
echo 'Vineet';
}
public function f2()
{
echo 'Saini';
}
}
$a = new test();
echo $a->f1();
echo $a->f2();
?>
Differences between abstract class and interface in PHP
Following are some main difference between abstract classes and interface in php.
1. In abstract classes this is not necessary that every method should be abstract. But in
interface every method is abstract.
2. Multiple and multilevel both type of inheritance is possible in interface. But single
and multilevel inheritance is possible in abstract classes.
3. Method of php interface must be public only. Method in abstract class in php could
be public or protected both.
4. In abstract class you can define as well as declare methods. But in interface you can
only defined your methods.

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

Php array
Php arrayPhp array
Php array
 
Object Oriented Programing JAVA presentaion
Object Oriented Programing JAVA presentaionObject Oriented Programing JAVA presentaion
Object Oriented Programing JAVA presentaion
 
PHP FUNCTIONS
PHP FUNCTIONSPHP FUNCTIONS
PHP FUNCTIONS
 
Form Handling using PHP
Form Handling using PHPForm Handling using PHP
Form Handling using PHP
 
Method Overloading in Java
Method Overloading in JavaMethod Overloading in Java
Method Overloading in Java
 
PHP - Introduction to File Handling with PHP
PHP -  Introduction to  File Handling with PHPPHP -  Introduction to  File Handling with PHP
PHP - Introduction to File Handling with PHP
 
Servlet and servlet life cycle
Servlet and servlet life cycleServlet and servlet life cycle
Servlet and servlet life cycle
 
Mvc architecture
Mvc architectureMvc architecture
Mvc architecture
 
Asp.net.
Asp.net.Asp.net.
Asp.net.
 
Object oreinted php | OOPs
Object oreinted php | OOPsObject oreinted php | OOPs
Object oreinted php | OOPs
 
Php forms
Php formsPhp forms
Php forms
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
 
MYSQL - PHP Database Connectivity
MYSQL - PHP Database ConnectivityMYSQL - PHP Database Connectivity
MYSQL - PHP Database Connectivity
 
Java 8 Lambda Expressions
Java 8 Lambda ExpressionsJava 8 Lambda Expressions
Java 8 Lambda Expressions
 
4.2 PHP Function
4.2 PHP Function4.2 PHP Function
4.2 PHP Function
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
Java database connectivity
Java database connectivityJava database connectivity
Java database connectivity
 
Uml class-diagram
Uml class-diagramUml class-diagram
Uml class-diagram
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
Php with MYSQL Database
Php with MYSQL DatabasePhp with MYSQL Database
Php with MYSQL Database
 

Destaque (7)

Functions in php
Functions in phpFunctions in php
Functions in php
 
Php tutorial
Php tutorialPhp tutorial
Php tutorial
 
Php Tutorials for Beginners
Php Tutorials for BeginnersPhp Tutorials for Beginners
Php Tutorials for Beginners
 
Class 3 - PHP Functions
Class 3 - PHP FunctionsClass 3 - PHP Functions
Class 3 - PHP Functions
 
PHP Functions & Arrays
PHP Functions & ArraysPHP Functions & Arrays
PHP Functions & Arrays
 
Functions in PHP
Functions in PHPFunctions in PHP
Functions in PHP
 
Sorting arrays in PHP
Sorting arrays in PHPSorting arrays in PHP
Sorting arrays in PHP
 

Semelhante a Abstract Class and Interface in PHP

SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptxSodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
RudranilDas11
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
Tech_MX
 

Semelhante a Abstract Class and Interface in PHP (20)

Lecture 10
Lecture 10Lecture 10
Lecture 10
 
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptxSodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
 
PHP 5
PHP 5PHP 5
PHP 5
 
Basic java important interview questions and answers to secure a job
Basic java important interview questions and answers to secure a jobBasic java important interview questions and answers to secure a job
Basic java important interview questions and answers to secure a job
 
Only oop
Only oopOnly oop
Only oop
 
JAVA PPT -3 BY ADI.pdf
JAVA PPT -3 BY ADI.pdfJAVA PPT -3 BY ADI.pdf
JAVA PPT -3 BY ADI.pdf
 
15 interfaces
15   interfaces15   interfaces
15 interfaces
 
Java Core
Java CoreJava Core
Java Core
 
Chap-3 Inheritance.pptx
Chap-3 Inheritance.pptxChap-3 Inheritance.pptx
Chap-3 Inheritance.pptx
 
06 abstract-classes
06 abstract-classes06 abstract-classes
06 abstract-classes
 
Abstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and InterfacesAbstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and Interfaces
 
this keyword in Java.pdf
this keyword in Java.pdfthis keyword in Java.pdf
this keyword in Java.pdf
 
Java OOPS Concept
Java OOPS ConceptJava OOPS Concept
Java OOPS Concept
 
Ganesh groups
Ganesh groupsGanesh groups
Ganesh groups
 
Java 06
Java 06Java 06
Java 06
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Unusual C# - OOP
Unusual C# - OOPUnusual C# - OOP
Unusual C# - OOP
 
SQL Joins
SQL JoinsSQL Joins
SQL Joins
 
Test Presentation
Test PresentationTest Presentation
Test Presentation
 
SQL Joins
SQL JoinsSQL Joins
SQL Joins
 

Mais de Vineet Kumar Saini

Mais de Vineet Kumar Saini (20)

Add edit delete in Codeigniter in PHP
Add edit delete in Codeigniter in PHPAdd edit delete in Codeigniter in PHP
Add edit delete in Codeigniter in PHP
 
Introduction to Html
Introduction to HtmlIntroduction to Html
Introduction to Html
 
Computer Fundamentals
Computer FundamentalsComputer Fundamentals
Computer Fundamentals
 
Country State City Dropdown in PHP
Country State City Dropdown in PHPCountry State City Dropdown in PHP
Country State City Dropdown in PHP
 
Pagination in PHP
Pagination in PHPPagination in PHP
Pagination in PHP
 
Stripe in php
Stripe in phpStripe in php
Stripe in php
 
Install Drupal on Wamp Server
Install Drupal on Wamp ServerInstall Drupal on Wamp Server
Install Drupal on Wamp Server
 
Joomla 2.5 Tutorial For Beginner PDF
Joomla 2.5 Tutorial For Beginner PDFJoomla 2.5 Tutorial For Beginner PDF
Joomla 2.5 Tutorial For Beginner PDF
 
Dropdown List in PHP
Dropdown List in PHPDropdown List in PHP
Dropdown List in PHP
 
Update statement in PHP
Update statement in PHPUpdate statement in PHP
Update statement in PHP
 
Delete statement in PHP
Delete statement in PHPDelete statement in PHP
Delete statement in PHP
 
Implode & Explode in PHP
Implode & Explode in PHPImplode & Explode in PHP
Implode & Explode in PHP
 
Types of Error in PHP
Types of Error in PHPTypes of Error in PHP
Types of Error in PHP
 
GET and POST in PHP
GET and POST in PHPGET and POST in PHP
GET and POST in PHP
 
Database connectivity in PHP
Database connectivity in PHPDatabase connectivity in PHP
Database connectivity in PHP
 
Arrays in PHP
Arrays in PHPArrays in PHP
Arrays in PHP
 
Programming in C
Programming in CProgramming in C
Programming in C
 
Browser information in PHP
Browser information in PHPBrowser information in PHP
Browser information in PHP
 
Operators in PHP
Operators in PHPOperators in PHP
Operators in PHP
 
Variables in PHP
Variables in PHPVariables in PHP
Variables in PHP
 

Último

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Último (20)

Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 

Abstract Class and Interface in PHP

  • 1. www.vineetsaini.wordpress.com Abstract Class & Interface in PHP Abstract Class Abstract classes are those classes which can not be directly initialized. Or in other word we can say that you can’t create object of abstract classes. Abstract classes always created for inheritance purpose. You can only inherit abstract class in your child class. Many peoples say that in abstract class at least your one method should be abstract. Abstract method are the method which is only defined but declared. But your any class has at least one method abstract than your class is abstract class. Abstract class are also known as base class. We call it base class because abstract class are not the class which is available directly for creating object. It can only act as parent class of any normal class. You can use abstract class in class hierarchy. It means one abstract class can inherit another abstract class also. We can create abstract classes in php using abstract keyword. Once we will make any class abstract in php we can’t create object of that class. 1. An abstract method contains no body. 2. An abstract method is not implemented by the base class. 3. There can be no objects of an abstract class. 4. An abstract class can inherit from a class and one or more interfaces. 5. An abstract class can implement code with non-Abstract methods. 6. An abstract class can have constructors or destructor's. 7. An abstract class cannot support multiple inheritance. <?php abstract class testParent { public function abc() { echo 'Vineet'; } } class testChild extends testParent { public function xyz() { echo 'Saini'; } } $a = new testChild(); echo $a->abc(); echo $a->xyz(); ?>
  • 2. www.vineetsaini.wordpress.com Interface We know that PHP does not support the multiple inheritance. For support multiple inheritance in php we use Interface. Interface is just like a class using interface keyword and contains only function declarations (function with no body). Function is defined in the classes where you call it. Interface gets useful when you need to extend a class from more than one parent class since class can’t do this. An interface cannot contain any functionality. It only contains definitions of the methods <?php interface template1 { public function f1(); } class test implements template1 { public function f1() { echo 'Vineet'; } public function f2() { echo 'Saini'; } } $a = new test(); echo $a->f1(); echo $a->f2(); ?> Differences between abstract class and interface in PHP Following are some main difference between abstract classes and interface in php. 1. In abstract classes this is not necessary that every method should be abstract. But in interface every method is abstract. 2. Multiple and multilevel both type of inheritance is possible in interface. But single and multilevel inheritance is possible in abstract classes. 3. Method of php interface must be public only. Method in abstract class in php could be public or protected both. 4. In abstract class you can define as well as declare methods. But in interface you can only defined your methods.