Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Max13's avatar
Level 3

Dummy class overridden by package implementation

We are a school doing IT among other things. I’m doing an intranet for the students and I want this intranet to be open source allowing the students to send PRs.

In this project I’m using a closed source code (a class with 1 method), so I’ve made it a private package but I would like the students to be able to clone the project without this package and the project still being usable. My idea is this: I implement a dummy class with the method always returning ‘true’, and when installing the package, a service provider would bind this dummy class to the package implementation.

Is it possible to do that without creating a common abstract class that the dummy and real implementation would both inherit from? Or am I thinking too complicated and there is something simpler?

Thanks for your help

0 likes
2 replies
LaryAI's avatar
Level 58

Certainly! You can achieve this by using Laravel's service container to conditionally bind the real implementation if the package exists, and otherwise, bind the dummy class. You don't necessarily need an abstract class; you can use an interface to define the contract for your class, and then have both your dummy and real classes implement this interface.

Here's a step-by-step solution:

  1. Define an interface that both your dummy and real classes will implement.
interface MyServiceInterface
{
    public function someMethod();
}
  1. Create the dummy class that implements the interface.
class DummyMyService implements MyServiceInterface
{
    public function someMethod()
    {
        return true;
    }
}
  1. In your service provider, conditionally bind the real implementation if the package exists, otherwise bind the dummy class.
use Illuminate\Support\ServiceProvider;

class MyServiceProvider extends ServiceProvider
{
    public function register()
    {
        if (class_exists('RealMyService')) {
            $this->app->bind(MyServiceInterface::class, RealMyService::class);
        } else {
            $this->app->bind(MyServiceInterface::class, DummyMyService::class);
        }
    }
}
  1. Now, you can type-hint the MyServiceInterface in your controllers or other classes where you need to use this service.
class SomeController extends Controller
{
    protected $myService;

    public function __construct(MyServiceInterface $myService)
    {
        $this->myService = $myService;
    }

    public function index()
    {
        $result = $this->myService->someMethod();
        // ...
    }
}

With this setup, Laravel's service container will automatically inject the correct implementation of MyServiceInterface depending on whether the real class exists or not. This allows your students to clone the project and work with the dummy implementation without needing the private package. When the private package is installed, the real implementation will be used instead.

Max13's avatar
Level 3

@LaryAI Is it possible, in the package, to rebind/override an interface already bound?

Please or to participate in this conversation.