Laravel 5.1: calling a function from a string

I am currently trying to call a function from a string.

This is the function that I will name later:

<?php
namespace App\Validation\Options;

class FacebookOptionValidation
{
    static public function validate()
    {
       echo: 'example';
       die();
    }
}

Here is my controller:

<?php
namespace App\Http\Controllers\Profile;

use App\Validation\Options;

class ProfileUserEditController extends Controller {

    public function updateUserOption()
    {
        $class = 'Options\FacebookOptionValidation';

        $class::validate();
    }
}

In this case, Laravel shows an error: \ FacebookOptionValidation 'class parameters not found

But when I call my function like this, everything works fine:

use App\Validation\Options;

class ProfileUserEditController extends Controller {

    public function updateUserOption()
    {
        Options\FacebookOptionValidation::validate();
    }
}

As mentioned here , you can call a class / function from a string. But in my case this is impossible - neither in the static, nor in the unsteady version.

Is this a "laravel-thing"?

+2
source share
2 answers

Try calling with a full namespace

    class ProfileUserEditController extends Controller {

        public function updateUserOption()
        {
            $class = 'App\Validation\Options\FacebookOptionValidation';

            $class::validate();


         }
}
+3

PHP7 :

(App\Validation\Options\FacebookOptionValidation::class)::validate();

+1

Source: https://habr.com/ru/post/1624291/


All Articles