dongmanzui8486 2017-06-09 09:07
浏览 18

object as exit()参数

Is this even valid? Because it's working.

if(empty($query)) {
    exit(View::forge('error', $data));
    //View::forge('error', $data) = object
}

According to PHP manual exit should only accept "string" or "int" as parameter.

So I tried:

if(empty($query)) {
    return View::forge('error', $data);  //will not display
}

    return View::forge('default', $data);  //displays

But it seems it ignores it and displays the default template, so I tried combining it with exit:

if(empty($query)) {
    return View::forge('error', $data);  //will not display
    exit;
}

return View::forge('default', $data);  //displays

But still the same result, what I want to know that is exit($obj) valid in fuelphp? Because it seems it's working.

What I want to do is if query is empty page would display error template instead of default template, any help would be appreciated, thanks!

  • 写回答

2条回答 默认 最新

  • dtng5978 2017-06-09 13:51
    关注

    If the object's result is a string or an integer then it fits the criteria set down in the manual. Whatever is within the exit brackets is executed as a PHP line; see the example below:

    $a = 4;
    $b = 17;
    exit(print $a * $b);
    

    this will output

    68

    so, on your question:

     exit(View::forge('error', $data));
    

    If the returned result of View::forge is a string or other printable output then it fits the criteria set out by exit and is executed accordingly.

    What will not work is a non-integer, non-string result such as:

    $a[] = "trees";
    $b[] = "cats";
    
    exit(array_merge($a,$b));
    

    Gives:

    Notice: Array to string conversion on line 4

    Due to this "conversion" I expect that if you pass an object to the exit function then it will probably try to use the __toString() class method, if available, (but I'm not certain on this yet).

    评论

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致