调用未定义的函数 App\Http\Controllers\ [ 函数名称 ]

2022-08-30 23:31:02

在我的控制器中,我创建了一个函数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()

有人可以教我如何解决这个错误吗?

提前非常感谢。


代码控制器.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!');

    }


}

答案 1

假设您在static getFactorialCodeController

那么这就是你需要调用静态函数的方式,因为静态属性和方法存在于类中,而不是存在于使用类创建的对象中。

CodeController::getFactorial($index);

----------------更新----------------

为了最佳实践,我认为您可以将这种函数放在一个单独的文件中,以便您可以更轻松地维护。

来做到这一点

在目录中创建一个文件夹并将其命名为(您可以输入您喜欢的名称)。applib

这个文件夹需要自动加载才能添加如下。并运行该命令。app/libcomposer.jsoncomposer dumpautoload

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

然后里面的文件将自动加载。lib

然后在里面创建一个文件,我给它起个名字libhelperFunctions.php

内部定义函数。

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;

     }
}

并将其在应用程序内的任何地方称为

$fatorial_value = getFactorial(225);

答案 2

如果它们位于同一控制器类中,则为:

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

否则,您需要创建该类的新实例,并调用该方法,即:

$controller = new MyController();
foreach ( $characters as $character) {
    $num += $controller->getFactorial($index) * $index;
    $index ++;
}

推荐