【发布时间】:2015-12-28 11:37:27
【问题描述】:
在我的控制器中,我创建了一个函数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()
有人可以教我如何解决这个错误吗?
提前非常感谢。
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!');
}
}
【问题讨论】:
-
在哪个类中定义了
getFactorial? -
CodeController 类。
-
static::getFactorial($index)。您应该阅读 OOP PHP 编程。 -
self::getFactorial($index)应该也可以。