php - Call to undefined function App\Http\Controllers\ [ function name ] -
in controller, create function getfactorial
public static function getfactorial($num) { $fact = 1; for($i = 1; $i <= $num ;$i++) $fact = $fact * $i; return $fact; }
then, use
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 $character) { $num += getfactorial($index) * $index; $index ++; } return redirect::to('/coding-puzzle') ->with('word', $word ) ->with('num', $num ) ->with('success','submit successfully!'); }
for reason, keep getting error
call undefined function app\http\controllers\getfactorial()
can please teach me how fix error ?
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 $character) { $num += getfactorial($index) * $index; $index ++; } return redirect::to('/coding-puzzle') ->with('word', $word ) ->with('num', $num ) ->with('success','submit successfully!'); } }
say define static getfactorial
function inside codecontroller
then way need call static function, because static properties , methods exists in class, not in objects created using class.
codecontroller::getfactorial($index);
----------------update----------------
to best practice think can put kind of functions inside separate file can maintain more easily.
to that
create folder inside app
directory , name lib
(you can put name like).
this folder needs autoload add app/lib
composer.json
below. , run composer dumpautoload
command.
"autoload": { "classmap": [ "app/commands", "app/controllers", ............ "app/lib" ] },
then files inside lib
autoloaded.
then create file inside lib
, name helperfunctions.php
inside define function.
if ( ! function_exists('getfactorial')) { /** * return factorial of number * * @param $number * @return string */ function getfactorial($date) { $fact = 1; for($i = 1; $i <= $num ;$i++) $fact = $fact * $i; return $fact; } }
and call anywhere within app as
$fatorial_value = getfactorial(225);
Comments
Post a Comment