0

I added a helper function file in composer.json right after the psr-4 entry

"autoload": {
    "psr-4": {
        "App\\": "app/",
        "Database\\Factories\\": "database/factories/",
        "Database\\Seeders\\": "database/seeders/"
    },
    "files": [
        "app/Http/Helpers/helpers.php"
    ]
},

but it throws a syntax error:

<?php

namespace App\Http\Helpers;

if (!function_exists('entity_url')) {
    function entity_show($entityType, $id, $locale = null) {
        if (is_null($locale) && function_exists('app')) {
            $locale = app()->getLocale();
        }

        $entity = \App\Models\{ $entityType }::find($id); // syntax error
    }
}

Parse error: syntax error, unexpected token "\"

If I use any existing actual Model it works: $entity = \App\Models\Foobar. I have several cases in my code where the complex curly syntax ({ $entityType }) is no problem, why is it here?


Edit

$class = '\\App\\Models\\' . $entityType; and $entity = "\\App\\Models\\$entityType"::find($id); work, I still dont understand why though. Throughout many controllers I can easily use \App\Models\{$entityType}::.

8
  • 2
    Are you sure this is a Laravel problem? If not, can you provide a minimal example that triggers this problem? Commented Feb 13, 2023 at 11:59
  • 1
    Also, you could build the class name first and then access the static method on it, like $class = '\App\Models\' . $entityType; $entity = $class::find($id); Commented Feb 13, 2023 at 12:02
  • Does this answer your question? PHP namespace with Dynamic class name Commented Feb 13, 2023 at 12:02
  • I think this syntax error occurs because you trying to use variable inside a string. try this way : $entity = '\App\Models\\' . $entityType; $entity = new $entity(); $entity = $entity::find($id); Commented Feb 13, 2023 at 12:03
  • @NicoHaase I dont know, Laravel Helpers are common and I wonder if this is specific to the Laravel bootstrapping process so I added the tags. The problem occurs at every request since the my helper is autoloaded and it appears to be a syntax problem. Commented Feb 13, 2023 at 12:06

1 Answer 1

1

There is a syntax error no matter where your code is.

The difference is that the helper file is loaded and compiled directly (autoloaded) while the controller class will only be loaded/compiled when called.

Means, if you take a class (User::class for example) and add that function to it.

class User
{
    public static function foo()
    {
        return 'bar';
    }

    public function entity_show($entityType, $id, $locale = null) {
        $entity = \App\Models\{ $entityType }::find($id); // syntax error
    }
}

If you run the command php artisan tinker it will not trigger the syntax error. But if in laravel tinker you run User::foo() the error will show up.

To dynamically load you class avoiding any syntax error, generate the class string in advance

if (!function_exists('entity_url')) {
    function entity_show($entityType, $id, $locale = null) {
        if (is_null($locale) && function_exists('app')) {
            $locale = app()->getLocale();
        }

        $entity = ('\App\Models\\' . $entityType)::find($id);
    }
}

Though I highly suggest you differ from using dynamic class calls since it will hinder searching for class uses when you want to change them. A simple way for your function is to pass the entity to be looked up in your function (which will also avoid having to check for the entity existence in DB inside this function)

use Illuminate\Database\Eloquent\Model;

if (!function_exists('entity_url')) {
    function entity_show(Model $entity, $locale = null) {
        if (is_null($locale) && function_exists('app')) {
            $locale = app()->getLocale();
        }

        //...
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.