在我的控制器中,我创建了一个函数getFactorial

public static function getFactorial($num)
{
    $fact = 1;
    for($i = 1; $i <= $num ;$i++)
        $fact = $fact * $i;
    return $fact;
}

然后,我就这样使用它

public function codingPuzzleProcess()
{

    $word     = strtoupper(Input::get('word'));
    $length   = strlen($word);
    $max_value = ($length * 26);
    $characters = str_split($word);

    $num = 1 ;
    $index = 1;

    sort($characters);

    foreach ( $characters as $character) {
        $num += getFactorial($index) * $index;
        $index ++;
    }

    return Redirect::to('/coding-puzzle')
        ->with('word', $word )
        ->with('num', $num )
        ->with('success','Submit successfully!');

}

出于某种原因,我不断地犯这个错误

Call to undefined function App\Http\Controllers\getFactorial()

有人能教我如何修复这个错误吗?

Much appreciated in advance.


CodeController.php

<?php

namespace App\Http\Controllers;
use View, Input, Redirect;

class CodeController extends Controller {


    public function codingPuzzle()
    {
        return View::make('codes.puzzle');
    }

    public static function getFactorial($num)
    {
        $fact = 1;
        for($i = 1; $i <= $num ;$i++)
            $fact = $fact * $i;
        return $fact;
    }


    public function codingPuzzleProcess()
    {

        $word     = strtoupper(Input::get('word'));
        $length   = strlen($word);
        $max_value = ($length * 26);
        $characters = str_split($word);

        $num = 1 ;
        $index = 1;

        sort($characters);

        foreach ( $characters as $character) {
            $num += getFactorial($index) * $index;
            $index ++;
        }

        return Redirect::to('/coding-puzzle')
            ->with('word', $word )
            ->with('num', $num )
            ->with('success','Submit successfully!');

    }


}

推荐答案

say you define the static getFactorial function inside a CodeController

then this is the way you need to call a static function, because static properties and methods exists with in the class, not in the objects created using the class.

CodeController::getFactorial($index);

----------------UPDATE----------------

To best practice I think you can put this kind of functions inside a separate file so you can maintain with more easily.

这么做

create a folder inside app directory and name it as lib (you can put a name you like).

this folder to needs to be autoload 这么做 add app/lib to composer.json as below. and run the composer dumpautoload command.

"autoload": {
    "classmap": [
                "app/commands",
                "app/controllers",
                ............
                "app/lib"
    ]
},

then files inside lib will autoloaded.

然后在lib内创建一个文件,我将其命名为helperFunctions.php

inside that define the function.

if ( ! function_exists('getFactorial'))
{

    /**
     * return the factorial of a number
     *
     * @param $number
     * @return string
     */
    function getFactorial($date)
    {
        $fact = 1;

        for($i = 1; $i <= $num ;$i++)
            $fact = $fact * $i;

        return $fact;

     }
}

and call it anywhere within the app as

$fatorial_value = getFactorial(225);

Laravel相关问答推荐

Laravel Livewire与自定义JS有关的多姆问题

如何在Laravel中从多行中获取合并数组?

spatie 包 laravel-tags 在迁移中没有 down() 函数是有原因的吗

如何自定义密码确认不匹配.错误信息?

Laravel phpunit 测试失败并出现 PDOException:SQLSTATE[HY000]:一般错误:1 接近0:语法错误

如何使用 Laravel Passport 在 API 中正确实现 OAuth?

错误try 访问 null 类型值的数组偏移量laravel 5.8.26 Php 7.4.2

Laravel 5.6 - 如何在 api 控制器中获取 auth()->user() 或 $response->user()?

Heroku 上的 Laravel 队列工作者

为什么人们将 .env 放入 gitignore?

Laravel 5 文件下载:stream() 或 download()

Laravel 5.3 Electron邮件队列中不允许序列化关闭

SQLSTATE [01000]:警告:1265 列的数据被截断(truncated)

Composer 致命错误:声明 Fxp... 必须与第 334 行的 ...AbstractAssetsRepository.php 兼容

安装后在 Lumen 中找不到页面

Laravel 验证 pdf mime

将 Laravel 集合附加到另一个集合

如何在 Laravel 或 Redis 中取消排队的作业(job)

如何判断是否在laravel中使用了分页

将参数传递给 Laravel 中的中间件