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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:47:38+00:00 2026-06-10T08:47:38+00:00

I am trying to use namespaces in php with some classes and interfaces. It

  • 0

I am trying to use namespaces in php with some classes and interfaces.

It appears that I must put a use statement for both the interface and the concrete type being used. This is kind of defeating the purpose of using interfaces surely?

So i may have

//Interface
namespace App\MyNamesapce;
interface MyInterface
{}

//Concrete Implementation
namespace App\MyNamesapce;
class MyConcreteClass implements MyInterface
{}

//Client
namespace App;
use App\MyNamespace\MyInterface  // i cannot do this!!!!
use App\MyNamespace\MyConcreteClass  // i must do this!
class MyClient
{}

Isnt the whole point of interfaces so that the concrete types are interchangeable – this goes against that surely. Unless i am not doing something correctly

  • 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-06-10T08:47:40+00:00Added an answer on June 10, 2026 at 8:47 am

    The concrete implementation IS interchangeable, but you need to specify somewhere which implementation you’d like to use, right?

    // Use the concrete implementation to create an instance
    use \App\MyNamespace\MyConcreteClass;
    $obj = MyConcreteClass();
    
    // or do this (without importing the class this time):
    $obj = \App\MyNamespace\MyConcreteClass2(); // <-- different concrete class!    
    
    class Foo {
        // Use the interface for type-hinting (i.e. any object that implements
        // the interface = every concrete class is okay)
        public function doSomething(\App\MyNamespace\MyInterface $p) {
            // Now it's safe to invoke methods that the interface defines on $p
        }
    }
    
    $bar = new Foo();
    $bar->doSomething($obj);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying use mod_rewrite to rewrite URLs from the following: http://www.site.com/one-two-file.php to http://www.site.com/one/two/file.php The
I'm trying to use namespaces. I want to extend a class inside a different
I am trying to use namespaces to declare an api. My routes.rb contains: devise_scope
I'm trying to implement autoloading in Php5.3 using namespaces but I'm having some issues
I'm using namespaces in PHP for the first time in an application that provides
Hi everyone Im trying to post some data to a php script using AppMancer
After doing some research about namespacing in PHP I'm trying to figure out what
I'm trying to read some RSS feeds with DOM in PHP like this one:
i'm trying use facebook API to upload photo in my fan page. I downloaded
I am trying use gem tire to search in my application. I have tables

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.