Interface or an Abstract Class which one to use

0 votes
Could you please explain when I should use an abstract class and when I should use a PHP interface?

What is the best way to convert my abstract class into an interface?
Aug 8, 2022 in PHP by Kithuzzz
• 38,000 points
897 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes
When you wish to require developers working on your system, including yourself, to implement a specific amount of methods on the classes they will be creating, use an interface.

When you want to compel developers working on your system, including yourself, to implement a predetermined number of methods and you want to offer some base methods to assist them in creating their child classes, use an abstract class.

Another thing to bear in mind is that client classes can implement numerous interfaces but can only extend one abstract class. Therefore, each child class can only adhere to one behaviour contract if you define your behaviour contracts as abstract classes. This can be advantageous in situations when you wish to steer your user-programmers in a specific direction. Sometimes it would be detrimental. The Countable and Iterator interfaces in PHP might theoretically be abstract classes rather than interfaces.

I hope this helps you.
answered Aug 9, 2022 by narikkadan
• 63,600 points

edited Mar 5

Related Questions In PHP

0 votes
0 answers

file_get_contents("php://input") or $HTTP_RAW_POST_DATA, which one is better to get the body of JSON request?

The file_get_contents("php://input") or $HTTP_RAW_POST_DATA - which one ...READ MORE

Jun 23, 2022 in PHP by narikkadan
• 63,600 points
1,311 views
0 votes
1 answer

How to check whether property exists in object or class in php?

Hello @kartik, Using  property_exists( mixed $class , string $property ...READ MORE

answered Oct 1, 2020 in PHP by Niroj
• 82,840 points
6,825 views
0 votes
1 answer

How to create an interface composed of other interfaces?

Hello, You are looking for the extends keyword: Interface IFoo extends ...READ MORE

answered Nov 5, 2020 in PHP by Niroj
• 82,840 points
562 views
0 votes
1 answer

Error:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'TYPE=MyISAM' at line 9

Hello @kartik, Replace TYPE=MyISAM with ENGINE=MyISAM The problem was "TYPE=MyISAM" which should ...READ MORE

answered Nov 16, 2020 in PHP by Niroj
• 82,840 points
11,491 views
0 votes
0 answers

How to echo or print an array in PHP?

I have this array: Array ( [data] => ...READ MORE

Jun 13, 2022 in PHP by narikkadan
• 63,600 points
446 views
0 votes
0 answers

When to use an interface instead of an abstract class and vice versa

This could be an OOP question in ...READ MORE

Jun 11, 2022 in C# by pranav
• 2,590 points
463 views
0 votes
0 answers

What is an abstract class in PHP?

What is an abstract class in PHP? ...READ MORE

Aug 1, 2022 in PHP by Kithuzzz
• 38,000 points
611 views
+1 vote
2 answers

Difference: abstract class and interface in Python

If abstract class contains only abstract methods ...READ MORE

answered Jun 21, 2019 in Python by anonymous
7,875 views
0 votes
1 answer

What is the difference between an interface and abstract class?

Interfaces An interface is a contract: The person writing ...READ MORE

answered Nov 30, 2020 in Python by Gitika
• 65,770 points
913 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP