0

Closure オブジェクトを少し操作するための基本的なクラスを作成しました。このアプリケーション/クロージャの動作を理解していないので、いくつか質問したいと思いました。現時点で私の心はかなり曇っているので、なぜ何かが動くのか、なぜ動かないのかわかりません.

<?php

class Route
{
     public static $bindings = array();
     public static $dispatch = array();

     public static function bind($bind)
     {
         self::$bindings[] = $bind;
     }

     public static function getAllBindings()
     {
         return (array) self::$bindings;
     }

     public static function get($binding, Closure $dispatch)
     {

         if(in_array($binding, self::$bindings))
         {
             if(is_callable($dispatch))
             {
                 return call_user_func($dispatch);
             }
             else
             {
                 die("Dispatch method is not callable.");
             }
         }
         else
         {
             die("Binding is not found in bindings array.");
         }
     }

     public static function test()
     {
         echo "Test ran!";
     }
 }

基本的に、バインディング (/admin、/account、/profile など) をバインドします。次に、クロージャーを使用してメソッドを呼び出します。

// Let's bind account and admin as available bindings
    Route::bind('account');
    Route::bind('admin');

// Let's try doing a get call with parameter "account"
    Route::get('account', function() { 
         // This is where I'm stuck. See below examples:
         // Route::test();
         // return "test";
         // return "testa";
         // return self::test();
    });

上記を確認した場合、ここに私の質問があります:

  1. 存在しないメソッドを指定するis_callableと、check が実行されず、php fatal error. is_callable存在しないメソッドをチェックするための有効なチェックではありませんか? なぜそれが起こるのですか?
  2. return "Test";クロージャーで指定すると、文字列$closure parameter in get methodが含まれ"Test"ますか?
  3. クロージャ内で異なるクラスのメソッドを渡すことはできますか? お気に入り:

    Route::get('account', function () {
        if(User::isLoggedIn() !== true)
             return Error::login_error('Unauthorized.');
    });
    
  4. もしそうなら、この呼び出しはどのスコープで行われていますか? PHP のクロージャーのスコープ、または call_user_func は、クロージャーを介して渡されるため、Route クラスのスコープ内でそれを呼び出しますか? (もう少し明確にするために、PHPのスコープはそうかもしれません$route->getが、クロージャースコープは使用するかもしれません$this->get
  5. var_dump/print_r のような Closure オブジェクトをダンプして内容を確認する方法はありますか?

短いガイダンスが私を動かします。私は PHP を知っていますが、クロージャーを使用することは私にとってかなり新しいことです。

どうもありがとうございました。返信に感謝します。

4

1 に答える 1

1

メソッド宣言の型ヒントが既にこれを保証しているため、そのis_callable()チェックは必要ありません。Closureまた、必要ありませんcall_user_func()。これにより、次のget()メソッドが得られます。

 public static function get($binding, Closure $dispatch)
 {

     if(!in_array($binding, self::$bindings))
     {
         die("Binding is not found in bindings array.");
     }

     return $dispatch();
 }

:現在、$bindingparam はチェックでのみ使用されますが、 への param としては使用されません$dispatch()。その理由がわかりません。この部分を再考する必要があります


あなたの投稿で別の隠された質問を見つけました:

// Let's try doing a get call with parameter "account"
Route::get('account', function() { 
    // This is where I'm stuck. See below examples:
    // Route::test();
    // return "test";
    // return "testa";
    // return self::test();
});

次のようになります。

// Let's try doing a get call with parameter "account"
Route::get('account', function() { 
    // Both should work well:
    // Route::test();
    // .. or 
    // return self::test();
});
于 2013-05-22T07:14:16.730 に答える