Jeroen de Beer
Jeroen de Beer

Reputation: 340

Insert path to class in composer, so I can remove it in "use" in php

In this case I have multiple footer classes. Now I have to edit the "Moduler" file to switch footers. Is it possible to declare the version of the footer in composer and change the "use" path to something more static like:

use Modules\Footer;

And specify the version of the footer in composer like:

App\\Helpers\\Moduler\\Modules\\Views\\Footer\\Footer__2\\Footer

This is the code I'm using right now

<?php namespace App\Helpers\Moduler;

use App\Helpers\Moduler\Modules\Views\Footer\Footer__2\Footer;

class Moduler
{
    use Footer;

    public function footer()
    {
        return $this->call_footer();
    }

    public static function instance()
    {
        return new Moduler();
    }
}

Upvotes: 0

Views: 64

Answers (1)

rob006
rob006

Reputation: 22174

You can use class aliases for this:

class_alias('App\Helpers\Moduler\Modules\Views\\ooter\Footer__2\Footer', 'Modules\Footer');

You can put this in some file and include it automatically using files setting in composer.json.

But honestly, it looks like really ugly magic and you (or someone else which will need to deal with that in future) will regret that. Use separate helper class and/or dependency injection instead - it will be more clear and predictable than magic traits driven by aliases.

Upvotes: 1

Related Questions