butschster / LaravelMetaTags

The most powerful and extendable tools for managing SEO Meta Tags in your Laravel project
MIT License
550 stars 52 forks source link

Typed property Butschster\Head\MetaTags\Meta::$config must not be accessed before initialization #69

Open jangaraev opened 1 month ago

jangaraev commented 1 month ago

Describe the bug I have this error in my script.

Typed property Butschster\Head\MetaTags\Meta::$config must not be accessed before initialization

this is how the code is organized in my project:

the controller (actually the trait, but it doesn't make sense here):

        use Butschster\Head\Facades\Meta;
        use Butschster\Head\Packages\Entities\OpenGraphPackage;
        use Butschster\Head\Packages\Package;

        ...

        $meta = new Package('default');
        $og = new OpenGraphPackage('default_og');

        $meta->setTitle('JohnDoe&Co')    // <-- ERROR IS THERE
                ->prependTitle($title);
        $og->setTitle(
            $title.' '.
            config('meta_tags.title.separator').' JohnDoe&Co'
        );
        $og->setSiteName('JohnDoe&Co');

        Meta::replacePackage($meta);
        Meta::replacePackage($og);

image

it's clearly seen that I instantiate an instance of \Butschster\Head\Packages\Package, which is okay, and it extends the \Butschster\Head\MetaTags\Meta which has a different declaration of constructor where the $config variable is expected.

apparently the thing is Package doesn't call the parent's constructor and that's why the private promoted property $config in Meta lefts undefined.

Environment:

Possible solution The easiest fix is to declare the $config variable in a regular way (not promoted), so the code looks like this

class Meta implements MetaInterface, \Stringable
...

    private ?Repository $config = null;

    public function __construct(
        protected Manager $packageManager,
        ?Repository $config = null,
    ) {
        $this->config = $config;
        $this->initPlacements();
    }
...

thanks for your open-source development

jangaraev commented 3 weeks ago

there is also an option to call parent's constructor in Butschster\Head\Packages\Package:

    public function __construct(
        protected string $name,
    ) {
        parent::__construct(app(ManagerInterface::class));
        $this->placements = new PlacementsBag();
    }

something like that

jangaraev commented 3 weeks ago

does anyone faced this issue? do I use a wrong approach, probably?