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

pigot4's avatar
Level 1

Optional parameters in component __construct

Hi guys

I have a Component that doesn't work fine.

class input extends Component
{
    public $name;
    public $value;
    public $step;
    public $required;

    public function __construct($name, $value = null, $step = null, $required = null)
    {
        $this->name = $name;
        $this->value = $value ?? '';
        $this->step = $step ?? '';
        $this->required = $required ?? 0;
    }
<input name="{{$name}}" value="{!!old($name, $value)!!}" {{$step ? 'step='.$step : ''}} {{$required ? 'required' : ''}}
<x-be.input label="{{__('Name')}}" name="name" class="w-1/2" />

So, I have a component with many parameters that could be null, and I defined that in the construct. On the local machine (MacOS), it works, but on the server (Ubuntu), Laravel returns the error "Undefined variable $value."

Where I'm wrong?

0 likes
8 replies
tykus's avatar

@pigot4 don’t know… did you restart PHP-FPM after deploying latest change(s)?

Aside, why do you default to null in the method signature, but set different default values on the properties?

1 like
pigot4's avatar
Level 1

@tykus Hello, the snippet I shared was just one of the many tests I had done. Originally, the code was much cleaner, but in my attempt to understand the issue, I made a series of changes, and what you noticed is just one of them. Clearly, now that I've found the solution, I'm cleaning up the code to make it correct.

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Probably not related, but linux is case sensitive (mac os is not normally). So there is a difference between Input and input. I notice your class is lowercase. Normally classes always start with a capital letter.

pigot4's avatar
Level 1

@Sinnbeck Hello, the problem wasn't case sensitivity, but you gave me a good lead to investigate the filesystem issue. Ultimately, it seems my server doesn’t like the folder named "be." Everything works correctly by moving the class file to a folder called "aa" (and, of course, updating the namespace and the related Blade templates).

I believe it’s a filesystem issue—there's something about that folder name that the system doesn't like. I arrived at this solution after noticing a Git log of a file rename: the log showed that both the folder and file names were changing, not just the file. I'm not sure how that’s possible, but it's as if the file name somehow includes the folder name.

JussiMannisto's avatar

I know this is off-topic, but why is the old() value unescaped? That can cause bugs and possibly security issues. Try submitting this value, for example:

"/><script>alert('Hello, world');</script>
1 like
pigot4's avatar
Level 1

@JussiMannisto The form will only be filled out by me, so I doubt anyone could perform an injection. Anyway, thanks for the suggestion because, even though I'm aware of the issue, there's a high risk of overlooking it and doing the same in the frontend. I need to get into the habit of not using old, so I'll avoid making mistakes, and that's a really good tip.

JussiMannisto's avatar

@pigot4 There's nothing wrong with using old(). What's dangerous is using it unescaped, i.e. {!! old('foo') !!} instead of the escaped form {{ old('foo') }}.

Please or to participate in this conversation.