原创作者: 马秉尧   阅读:1288次   评论:0条   更新时间:2011-06-01    
直接通过远程方法名进行远程调用

在快速入门一章中,我们已经见识过这种方式的调用了,这里再来具一个例子来进行说明:
<?php
include("hprose/hproseHttpClient.php");
$client = new HproseHttpClient("http://www.hprose.com/example/");
echo "<pre>";
echo $client->sum(1, 2, 3, 4, 5);
echo "\r\n";
echo $client->Sum(6.0, 7.0, 8.0);
echo "\r\n";
$userList = $client->getUserList();
print_r($userList);
echo "</pre>";
?>


这个例子的运行结果是:
引用
15
21
Array
(
    [0] => User Object
        (
            [name] => Amy
            [sex] => 2
            [birthday] => HproseDate Object
                (
                    [year] => 1983
                    [month] => 12
                    [day] => 3
                    [utc] =>
                )

            [age] => 26
            [married] => 1
        )

    [1] => User Object
        (
            [name] => Bob
            [sex] => 1
            [birthday] => HproseDate Object
                (
                    [year] => 1989
                    [month] => 6
                    [day] => 12
                    [utc] =>
                )

            [age] => 20
            [married] =>
        )

    [2] => User Object
        (
            [name] => Chris
            [sex] => 0
            [birthday] => HproseDate Object
                (
                    [year] => 1980
                    [month] => 3
                    [day] => 8
                    [utc] =>
                )

            [age] => 29
            [married] => 1
        )

    [3] => User Object
        (
            [name] => Alex
            [sex] => 3
            [birthday] => HproseDate Object
                (
                    [year] => 1992
                    [month] => 6
                    [day] => 14
                    [utc] =>
                )

            [age] => 27
            [married] =>
        )

)


从这个例子中,我们可以看出通过远程方法名进行调用时,远程方法名是不区分大小写的,所以不论是写sum还是Sum都可以正确调用,如果远程方法返回结果中包含有某个类的对象,而该类并没有在客户端明确定义的话,Hprose会自动帮你生成这个类的定义(例如上例中的User类),并返回这个类的对象。

通过Invoke方法进行远程调用

非引用参数传递

上面介绍的通过远程方法名进行远程调用的例子就是非引用参数传递,下面是用invoke方法重写的代码:
<?php
include("hprose/hproseHttpClient.php");
$client = new HproseHttpClient("http://www.hprose.com/example/");
echo "<pre>";
$args = array(1, 2, 3, 4, 5);
echo $client->invoke("sum", $args);
echo "\r\n";
$args = array(6.0, 7.0, 8.0);
echo $client->invoke("Sum", $args);
echo "\r\n";
$userList = $client->invoke("getUserList");
print_r($userList);
echo "</pre>";
?>

运行结果与上面例子的运行结果完全相同。但是我们发现用invoke方法并不方便,因为当有参数时,必须要把参数单独放入一个变量中才可以进行传递。所以通常我们无需直接使用invoke方法,除非我们需要动态调用。另外,还有一种情况下,你会用到invoke方法,那就是在进行引用参数传递时。

引用参数传递

下面这个例子很好的说明了如何进行引用参数传递:
<?php
include("hprose/hproseHttpClient.php");
$client = new HproseHttpClient("http://www.hprose.com/example/");
echo "<pre>";
$args = array(array("Mon"=>1,"Tue"=>2,"Wed"=>3,"Thu"=>4,"Fri"=>5,"Sat"=>6,"Sun"=>7));
echo "args(before invoke):\r\n";
print_r($args);
$result = $client->invoke("swapKeyAndValue", $args, true);
echo "args(after invoke):\r\n";
print_r($args);
echo "result:\r\n";
print_r($result);
echo "</pre>";
?>

上面程序的运行结果如下:
引用
args(before invoke):
Array
(
    [0] => Array
        (
            [Mon] => 1
            [Tue] => 2
            [Wed] => 3
            [Thu] => 4
            [Fri] => 5
            [Sat] => 6
            [Sun] => 7
        )

)
args(after invoke):
Array
(
    [0] => Array
        (
            [1] => Mon
            [2] => Tue
            [3] => Wed
            [4] => Thu
            [5] => Fri
            [6] => Sat
            [7] => Sun
        )

)
result:
Array
(
    [1] => Mon
    [2] => Tue
    [3] => Wed
    [4] => Thu
    [5] => Fri
    [6] => Sat
    [7] => Sun
)

我们看到运行前后,$args中的值已经改变了。

这里有一点要注意,当参数本身是数组时,该数组应该作为参数数组的第一个元素传递,例如上例中的$args是:
array(array("Mon"=>1,"Tue"=>2,"Wed"=>3,"Thu"=>4,"Fri"=>5,"Sat"=>6,"Sun"=>7))
而不能只写:
array("Mon"=>1,"Tue"=>2,"Wed"=>3,"Thu"=>4,"Fri"=>5,"Sat"=>6,"Sun"=>7)

否则程序将会出错,或者在调用中陷入等待状态,这样的错误不容易被找到,因此一定要注意这一点。
评论 共 0 条 请登录后发表评论

发表评论

您还没有登录,请您登录后再发表评论

文章信息

  • andot在2010-01-26创建
  • andot在2011-06-01更新
  • 标签: hprose, php
Global site tag (gtag.js) - Google Analytics