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

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -