Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jangaraev opened this issue Oct 3, 2024 · 2 comments

Comments

@jangaraev
Copy link

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:

  • PHP: 8.3.6
  • Laravel: 11.26.0
  • Package: 3.1.1

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
Copy link
Author

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
Copy link
Author

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

jangaraev added a commit to jangaraev/LaravelMetaTags that referenced this issue Nov 6, 2024
jangaraev added a commit to jangaraev/LaravelMetaTags that referenced this issue Nov 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant