php 调用未定义的函数 App\Http\Controllers\ [函数名]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32870243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call to undefined function App\Http\Controllers\ [ function name ]
提问by cyber8200
In my controller, I create a function getFactorial
在我的控制器中,我创建了一个函数 getFactorial
public static function getFactorial($num)
{
$fact = 1;
for($i = 1; $i <= $num ;$i++)
$fact = $fact * $i;
return $fact;
}
Then, I use it like this
然后,我像这样使用它
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!');
}
For some reason, I keep getting this error
出于某种原因,我不断收到此错误
Call to undefined function App\Http\Controllers\getFactorial()
Call to undefined function App\Http\Controllers\getFactorial()
Can someone please teach me how to fix this error ?
有人可以教我如何解决这个错误吗?
Much appreciated in advance.
非常感谢提前。
CodeController.php
代码控制器.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!');
}
}
采纳答案by Kalhan.Toress
say you define the static getFactorial
function inside a CodeController
假设你static getFactorial
在 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.
为了最佳实践,我认为您可以将此类功能放在单独的文件中,以便您可以更轻松地进行维护。
to do that
要做到这一点
create a folder inside app
directory and name it as lib
(you can put a name you like).
在app
目录中创建一个文件夹并将其命名为lib
(您可以输入您喜欢的名称)。
this folder to needs to be autoload to do that add app/lib
to composer.json
as below. and run the composer dumpautoload
command.
此文件夹需要自动加载才能添加app/lib
到composer.json
如下内容中。并运行composer dumpautoload
命令。
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
............
"app/lib"
]
},
then files inside lib
will autoloaded.
然后里面的文件lib
会自动加载。
then create a file inside lib
, i name it helperFunctions.php
然后在里面创建一个文件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);
回答by Arkitecht
If they are in the same controller class, it would be:
如果它们在同一个控制器类中,它将是:
foreach ( $characters as $character) {
$num += $this->getFactorial($index) * $index;
$index ++;
}
Otherwise you need to create a new instance of the class, and call the method, ie:
否则,您需要创建该类的新实例,并调用该方法,即:
$controller = new MyController();
foreach ( $characters as $character) {
$num += $controller->getFactorial($index) * $index;
$index ++;
}