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

valentin_vranic's avatar

Typed property must not be accessed before initialization

I know it's a billion dollar question, asked so many times, but still I can't figure it out. Everything worked seamlessly until I pushed my code to development server.

I was following these instructions: https://livewire.laravel.com/docs/forms#extracting-a-form-object

Livewire - v3.4.9

Laravel - v10.48.3

Typed property App\Livewire\Siptrunk\SiptrunkModal::$form must not be accessed before initialization
public ?SipTrunksForm $form;

public function mount(?SipTrunk $sipTrunk): void
{
    if ($sipTrunk->exists) {
        $this->form->setSipTrunk($sipTrunk);
    }
}

When I remove type, it's referencing as null for $this->form , and fails.

0 likes
7 replies
LaryAI's avatar
Level 58

It seems like you're encountering an issue where the typed property $form is being accessed before it has been initialized. In PHP, typed properties must be initialized before you can access them, otherwise, you'll get the error you're seeing.

To solve this issue, you need to ensure that the $form property is initialized before you try to use it. You can do this by either providing a default value when you declare the property or by initializing it in the mount method before you access it.

Here's how you can modify your mount method to ensure $form is initialized:

public ?SipTrunksForm $form = null;

public function mount(?SipTrunk $sipTrunk): void
{
    $this->form = new SipTrunksForm(); // Initialize the form property

    if ($sipTrunk && $sipTrunk->exists) {
        $this->form->setSipTrunk($sipTrunk);
    }
}

In the above code, I've added a default value of null to the $form property declaration. Then, in the mount method, I've initialized $form with a new instance of SipTrunksForm before checking if $sipTrunk exists and setting it.

This should resolve the error you're encountering. If SipTrunksForm requires any parameters for its constructor, make sure to pass them when creating the new instance.

valentin_vranic's avatar

@LaryAI I'm not sure how to pass the required parameters for the constructor since it asks for $component and $propertyName

new SipTrunksForm($component, $propertyName)

martinbean's avatar

@valentin_vranic

This reply was automagically generated by our local A.I. Think of it as a one-off attempt to instantly solve your problem. It will not respond to further replies.

jaseofspades88's avatar

Do you have any computed properties, methods or blade references to the form, which are attempting to access the property before it has been initialised?

Likely culprits are $form->value in your blade or $this->form->something() in a computed property somewhere.

There will definitely be something happening, which causes a conflict in these lifecycle hooks: https://livewire.laravel.com/docs/lifecycle-hooks

valentin_vranic's avatar

@jaseofspades88 well, it's screaming as I noted in the question, in mount, for:

$this->form->setSipTrunk($sipTrunk);

But it's strange for me that in my dev environment everything is just working fine, with very identical configs.

valentin_vranic's avatar

Okay, this was a bit stupid, but finally I've found the issue.

On include I had a typo: instead of

use App\Livewire\Forms\SipTrunksForm; I had use App\Livewire\Forms\SiptrunksForm;

It seams that PHPStorm accepted it, but the application failed.

Snapey's avatar

your local environment might be case insensiive.

By the way, Lary knows NOTHING about Livewire 3 and Form objects so you can almost always ignore his advice for Livewire 3 related issues.

3 likes

Please or to participate in this conversation.