Skip to content
This repository has been archived by the owner on Jul 16, 2021. It is now read-only.

Invoking Blade Component Methods #2129

Open
imliam opened this issue Mar 14, 2020 · 0 comments
Open

Invoking Blade Component Methods #2129

imliam opened this issue Mar 14, 2020 · 0 comments

Comments

@imliam
Copy link

imliam commented Mar 14, 2020

I really don't like the way Blade component methods are invoked without parenthesis when there are no arguments. It feels like such a hack and is inconsistent for a couple of different reasons.

I'm sure it won't change in 7.x now but would love to see it changed in the future to be nicer to use. I'm curious about what other people think too.

Personally I'd like to see one simple (although backwards incompatible) change made: methods always need parenthesis in the Blade template to be invoked.


Here are my reasons for not liking the current behaviour:

1: You can't choose to consistently use parenthesis for methods. Returning a string from the method and then using parenthesis will attempt to call a function with the name of the returned string.

class ExampleComponent extends Component
{
    public function something() {
        return 'somethingElse';
    }

    public function render() {
        return '<div {{ $something() }}></div>'; // Triggers: "Call to undefined function somethingElse()"
    }
}

2: It's not immediately obvious what'll happen if you have both a property and method of the same name:

class ExampleComponent extends Component
{
    public $something;

    public function something() { ... }
}

3: It's not immediately obvious how you should interact with a method that has a nullable parameter. $something, $something() or $something(null) could all be equally valid in some way, but mean different things in the context of Blade components.

class ExampleComponent extends Component
{
    public function something($param = null) { ... }
}

I don't want to have to think so deeply about how I'm using something when it could've just been simplified and behave like normal PHP. Every time I do something with a Blade component now, a chunk of my time goes into considering what behaviour is going to come out of a method.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant