Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Flight PHP上将变量传递给obejct方法_Php_Rest_Flightphp - Fatal编程技术网

在Flight PHP上将变量传递给obejct方法

在Flight PHP上将变量传递给obejct方法,php,rest,flightphp,Php,Rest,Flightphp,根据,使用对象方法是通过使用: Flight::route('/some/route', [$object, 'method']); Flight::route('/@name/@id', function($name, $id){ echo "hello, $name ($id)!"; }); 要使用路线参数,请使用: Flight::route('/some/route', [$object, 'method']); Flight::route('/@name/@id', fu

根据,使用对象方法是通过使用:

Flight::route('/some/route', [$object, 'method']);
Flight::route('/@name/@id', function($name, $id){
    echo "hello, $name ($id)!";
});
要使用路线参数,请使用:

Flight::route('/some/route', [$object, 'method']);
Flight::route('/@name/@id', function($name, $id){
    echo "hello, $name ($id)!";
});
我试着这样把两者结合起来:

Flight::route('/user/@id', [$object, 'method']);

但它不起作用。有没有办法将参数传递给对象方法?

在闭包中分配变量如何

Flight::route('/@name/@id', function($name, $id){
    $obj = new Object; // or use a DIC
    $obj->name = $name;
    $obj->id = $id; // or assign these in the constructor
});

在闭包中分配变量怎么样

Flight::route('/@name/@id', function($name, $id){
    $obj = new Object; // or use a DIC
    $obj->name = $name;
    $obj->id = $id; // or assign these in the constructor
});

查看(方法
callFunction
invokeMethod
),您的用例应该是受支持的。匿名函数和类方法中应该同样支持参数…

看看(方法
callFunction
invokeMethod
),您的用例应该得到支持。匿名函数和类方法中应该同样支持参数…

这段代码适合我:

function someFunction($id) {
  echo 'id: ' . $id;
}

class SomeClass {
    function method1($id) {
      echo 'Class, id: ' . $id;
    }
    function method2($name, $id) {
      echo 'Class, name: ' . $name . ', id: ' . $id;
    }
}
$object = new SomeClass();

Flight::route('/user/@id', array($object, 'method1'));
Flight::route('/user/@id/@name', array($object, 'method2'));
Flight::route('/fun/@id', 'someFunction');
我不擅长PHP,但这是一种回调:

此代码适用于我:

function someFunction($id) {
  echo 'id: ' . $id;
}

class SomeClass {
    function method1($id) {
      echo 'Class, id: ' . $id;
    }
    function method2($name, $id) {
      echo 'Class, name: ' . $name . ', id: ' . $id;
    }
}
$object = new SomeClass();

Flight::route('/user/@id', array($object, 'method1'));
Flight::route('/user/@id/@name', array($object, 'method2'));
Flight::route('/fun/@id', 'someFunction');
我不擅长PHP,但这是一种回调: