Laravel 5.1:从字符串调用函数 [英] Laravel 5.1: Calling a function from string

查看:84
本文介绍了Laravel 5.1:从字符串调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我正在尝试从字符串中调用函数.

Currently I'm trying to call a function from a string.

这是函数,我将在以后调用:

This is the function, that I'll call later:

<?php
namespace App\Validation\Options;

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

这是我的控制人:

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

use App\Validation\Options;

class ProfileUserEditController extends Controller {

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

        $class::validate();
    }
}

在这种情况下,Laravel显示错误: 未找到"Options \ FacebookOptionValidation"类

In this case Laravel shows an error: Class 'Options\FacebookOptionValidation' 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, it's possible to call a class/function from a string. But in my case it's not possible - neither in the static or non-static variant.

那是幼虫"吗?

推荐答案

尝试使用完整名称空间进行呼叫

Try call with full namespace

    class ProfileUserEditController extends Controller {

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

            $class::validate();


         }
}

这篇关于Laravel 5.1:从字符串调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆