【问题标题】:Php helper function parameter undefinedphp辅助函数参数未定义
【发布时间】:2017-07-18 09:28:44
【问题描述】:

在我的 Laravel 5.4 项目中,我有一个 Helpers.php 文件。这很好用。

现在我制作了一个看起来像这样的助手:

if (! function_exists('issetWithReturn')) {
    /**
     * @return mixed
     */
    function issetWithReturn($values)
    {
        return isset($collection) ? $collection : '';
    }
}

在我的OrganisationController.php 我这样使用它:

/**
 * Show all organisations.
 *
 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
 */
public function index()
{
    if (Gate::allows('edit-organisations')) {
        $products = $this->productRepo->getAll();
    }

    return view('organisation.index')->with([
        'products'   => issetWithReturn($products),
    ]);
}

但是我的编辑器已经给出了issetWithReturn 中的$products 未定义的标志?这是为什么呢?

当我尝试这个时,一切正常:

'products'   => isset($products) ? $products : '',

【问题讨论】:

  • 你是否包含了帮助文件?
  • 是的,我使用了更多的辅助函数并且它们工作正常。它在我的composer.json 中。我跑了composer dumpautoload -o。但这不是问题。

标签: php laravel helper


【解决方案1】:

你传递了一个名为 $values 的参数,但在函数内部使用了一个名为 $collection 的变量

所以基本上是一个错字

if (! function_exists('issetWithReturn')) {
    /**
     * @return mixed
     */
    function issetWithReturn($values)
    {
        return isset($value) ? $value : '';
    }
}

【讨论】:

    猜你喜欢
    • 2018-12-13
    • 2015-06-24
    • 1970-01-01
    • 2019-11-10
    • 1970-01-01
    • 2019-11-25
    • 2020-09-22
    • 2016-04-24
    • 1970-01-01
    相关资源
    最近更新 更多