0

何らかの理由で、change()関数と停止後のすべてを実行できません。試してみまし$this->change()たが、効果は同じです。関数をphpファイルで実行すると、機能し、数値が変化します。

class Test extends CI_Controller {

    function __construct(){
        parent::__construct();
    }

    public function home(){

        $num = 1;

        change($num);


        function change($num,$rand=false){

            echo 'inside function'; // this is not shown

            if($num < 4) {
                $rand = rand(1,9);
                $num = $num.$rand;
                change($num,$rand);
            } else {
                echo $num;
            }
        }
        echo 'done'; // this is not shown

    }

}
4

2 に答える 2

4

おそらく、データを処理するためにプライベートまたはパブリック関数を呼び出す方が良いでしょう(または、より複雑な/関与するプロセスの場合はライブラリ)。

すなわち

class Test extends CI_Controller
{
  public function __construct()
  {
      parent::__construct();
  }

  public function home()
  {
    $num = 1;

    echo $this->_change($num);
    echo 'done'; // this is not shown
  }

  // private or public is w/o the underscore
  // its better to use private so that CI doesn't route to this function
  private function _change($num, $rand = FALSE)
  {
        if($num < 4) {
            $rand = rand(1,9);
            $num = $num + $rand;
            $this->_change($num,$rand);
        } else {
            return $num;
        }
  }
}
于 2012-10-19T15:53:24.607 に答える
3

笑、関数内に関数を記述しようとしています。

このコードでクラスを実行してみてください

class Test extends CI_Controller {

    function __construct(){
        parent::__construct();
    }

    public function home(){

        $num = 1;

        $this->change($num);
        echo 'done'; // this is not shown

    }

    public function change($num,$rand=false){

            echo 'inside function'; // this is not shown

            if($num < 4) {
                $rand = rand(1,9);
                $num = $num.$rand;
                change($num,$rand);
            } else {
                echo $num;
            }
    }

}
于 2012-10-19T15:52:33.203 に答える