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 1075489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:16:44+00:00 2026-05-16T21:16:44+00:00

I have studied in php oop and stocked in the concept of reusable code.

  • 0

I have studied in php oop and stocked in the concept of reusable code.

I have seen an example like

interface iTemplate
{
    public function setVariable($name, $var);
    public function getHtml($template);
}
And implement it:

// Implement the interface
class Template implements iTemplate
{
    private $vars = array();

    public function setVariable($name, $var)
    {
        $this->vars[$name] = $var;
    }

    public function getHtml($template)
    {
        foreach($this->vars as $name => $value) {
            $template = str_replace('{' . $name . '}', $value, $template);
        }

        return $template;
    }
} 

I can understand the code but not sure why it is reusable. Every time I want to add a new function in iTemplate interface, my Template class needs to be changed too. I don’t understand the concept of “reuse”. I appreciate any help. Thanks.

  • 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-16T21:16:44+00:00Added an answer on May 16, 2026 at 9:16 pm

    Interfaces aren’t directly for code reuse. They are for abstraction. They enable classes that use the template to check for the interface instead of the base template class. That way it separates implementation from the interface declaration.

    So if your method does something with a template class, checking for an object of instance template would hard code a dependency on that class. But in reality you don’t care what class you get, you just care if it adheres to the iTemplate interface (since that’s all you’re calling anyway).

    public function foo(Template $template) {
    

    vs:

    public function foo(iTemplate $template) {
    

    Now, as far as code re-use, interfaces aren’t really designed for that. Inheritance typically is. Basically think of inheritance as extending an abstraction. Let me give you an example:

    If you were to create a set of classes for birds, you could approach it with inheritance and without it. Let’s see how we might do it without:

    interface iBird {
        public function fly();
        public function speak();
        public function swim();
        public function walk();
    }
    
    class Duck implements iBird {
        public function fly() {
            //Fly here
        }
        public function speak() {
            // Quack here
        }
        public function swim() {
            //Swim here
        }
        public function walk() {
            //Walk here
        }
    }
    
    class Turkey implements iBird {
        public function fly() {
            //Fly here, but limited
        }
        public function speak() {
            //Make turkey sound here
        }
        public function swim() {
            throw new Exception('Turkeys can not swim!');
        }
        public function walk() {
            //Walk here
        }
    }  
    

    Now, this is a simple example, but you can see that in those two birds, the walk() functions will likely be identical (and hence violate DRY)…

    Let’s see how that might look with a single tier inheritance:

    abstract class Bird implements iBird {
        public function fly() {
            //Fly here
        }
        abstract public function speak();
        public function swim() {
            //Swim here
        }
        public function walk() {
            //Walk here
        }
    }
    
    class Duck extends Bird {
        public function speak() {
            //Quack here
        }
    } 
    
    class Turkey extends Bird {
        public function speak() {
            //Make turkey sound here
        }
        public function swim() {
            throw new Exception('Turkeys can not swim!');
        }
    }  
    

    Now, you can see we just re-used 3 of the methods! We didn’t declare speak(), since it will be always overriden (since no two birds sound alike).

    Sounds good right? Well, depending on our needs, we may want to add other abstract types. So lets say we were making a lot of different types of birds… We would have some that didn’t swim, so we might create an abstract class NonSwimmingBird that extends Bird, but throws the exception for us. Or a NonFlyingBird, or a ShortRangeBird…

    Now, we’re really on the track as far as code re-use, but we’re hitting a wall in another area. Suppose we have a bird that doesn’t fly or swim. What class do we inherit from? Either way, we’re duplicating code. So we need to find another way out. Well, how do we do it? Through Design Patterns… Instead of direct inheritance, we could use a decorator pattern to add those traits on the fly. (There are other patterns that can be used here, the point is to show that inheritance alone won’t suit all needs. And Patterns alone won’t either. You need a good architecture using both worlds based upon what your exact needs are)…

    The point is, it all depends on your needs. If you only have 2 “classes” of objects you’re going to architect something much simpler than if you are planning on having thousands. The point of what I wrote here is to demonstrate how you can use straight inheritance to enforce some DRY principals (but also how straight inheritance can cause code duplication as well). The big thing, is don’t try to stick to DRY just because you don’t want to repeat yourself. Stick to DRY, but make sure that you’re combining and extending where it’s reasonable to, otherwise you’re creating yourself a maintenance headache. Stick to the Single Responsibility Principal, and you should be fine…

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

Sidebar

Related Questions

I've just read tutorial about enums and have one question. I've studied example: public
i have studied design patterns and want to use them to code an open
I have studied that during a fork, the data and code segment of the
I've studied C programming in college some years ago and have developed some medium
Have you ever seen any of there error messages? -- SQL Server 2000 Could
Have you guys had any experiences (positive or negative) by placing your source code/solution
I'm trying to learn how to best use OOP in PHP. Please be aware
I have a partition, formatted as NTFS. I have studied that devices formatted as
I have a snippet to create a 'Like' button for our news site: <iframe
I have found this example on StackOverflow: var people = new List<Person> { new

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.