Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1025991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:00:48+00:00 2026-05-16T12:00:48+00:00

Possible Duplicate: PHP: What is the difference between an interface and abstract class? As

  • 0

Possible Duplicate:
PHP: What is the difference between an interface and abstract class?

As far as I understand, a class implements or extends abstract or interface class has to use the default methods. I know we can use implement keyword to use multiple interfaces, but we only can extend 1 abstract. Which one to use in real life project and the difference?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T12:00:49+00:00Added an answer on May 16, 2026 at 12:00 pm

    The differences are both theoretical and practical:

    • interface is a description of some capability your class has and advertises (so various classes implementing the same interface can be used the same way)
    • abstract class can be a default implementation, containing the parts which are likely to appear in all the implementations. It doesn’t have to implement the complete interface

    Example – an interface:

    // define what any class implementing this must be capable of
    interface IRetrieveData {
        // retrieve the resource
        function fetch($url);
    
        // get the result of the retrieval (true on success, false otherwise)
        function getOperationResult();
    
        // what is this class called?
        function getMyClassName();
    }
    

    Now we have the set of requirements that will be checked for every class implementing this. Let’s make an abstract class and its children:

    // define default behavior for the children of this class
    abstract class AbstractRetriever implements IRetrieveData {
        protected $result = false;
    
        // define here, so we don't need to define this in every implementation
        function getResult() {
           return $result;
        }
    
        // note we're not implementing the other two methods,
        // as this will be very different for each class.
    }
    
    class CurlRetriever extends AbstractRetriever {
         function fetch($url) {
             // (setup, config etc...)
             $out = curl_execute();
             $this->result = !(curl_error());
             return $out;
         }
         function getMyClassName() {
             return 'CurlRetriever is my name!';
         }
    }
    
    class PhpRetriever extends AbstractRetriever {
         function fetch($url) {
            $out = file_get_contents($url);
            $this->result = ($out !== FALSE);
            return $out;
         }
         function getMyClassName() {
             return 'PhpRetriever';
         }
    }
    

    A completely different abstract class (unrelated to the interface), with a subclass which implements our interface:

    abstract class AbstractDog {
         function bark() {
             return 'Woof!'; 
         }
    }
    
    class GoldenRetriever extends AbstractDog implements IRetrieveData {
         // this class has a completely different implementation
         // than AbstractRetriever
         // so it doesn't make sense to extend AbstractRetriever
         // however, we need to implement all the methods of the interface
         private $hasFetched = false;
    
         function getResult() {
             return $this->hasFetched;
         }
    
         function fetch($url) {
             // (some retrieval code etc...)
             $this->hasFetched = true;
             return $response;
         }
         function getMyClassName() {
             return parent::bark();
         }
    }
    

    Now, in other code, we can do this:

    function getStuff(IRetrieveData $retriever, $url) {
        $stuff = $retriever->fetch($url);
    }
    

    and we don’t have to worry which of the retrievers (cURL, PHP, or Golden) will be passed in, and how are they going to accomplish the goal, as all should be capable of behaving similarly. You could do this with an abstract class, too, but then you’re restricting yourself based on the classes’ ancestor, instead of its capability.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 500k
  • Answers 500k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It's a connection timeout - if you're not having a… May 16, 2026 at 1:56 pm
  • Editorial Team
    Editorial Team added an answer Tell it to ignore mouse events. May 16, 2026 at 1:56 pm
  • Editorial Team
    Editorial Team added an answer Assuming you're using NHibernate with SharpArchitecture (since that's the happy… May 16, 2026 at 1:56 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Possible Duplicate: PHP: different quotes? Simple question: What is the difference between ' and
Possible Duplicate: How to calculate the difference between two dates using PHP? I have
Possible Duplicate: Interface vs Abstract Class (general OO) Hi guys, I decided to dig
Possible Duplicate: Difference between void main and int main? Alright, so I'm using bloodshed
Possible Duplicate: Using Memcache vs Memcached with PHP Someone can explain me the difference
Possible Duplicate: PHP PDO vs normal mysql_connect So in my application, I accessed the
Possible Duplicate: Reference: Comparing PHP's print and echo Is there any major and fundamental
Possible Duplicate: How are echo and print different in PHP? UPDATE : I found
Possible Duplicate: PHP 5.3 changelog? I am new to PHP, and I am confused
Possible Duplicate: PHP: How To Disable Dangerous Functions Hi, this is my situation: I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.