dongyu1918 2013-10-09 02:58
浏览 49

如何判断链中的最后一个方法?

I've started using method chains in my PHP app:

$object->do()->something();

Specifically with querying databases:

$database->select()->from('mytable');

However at the moment I need to call a function to actually perform the query at the end:

$database->select()->from('mytable')->perform();

This is annoying because usually every chain will perform straight away. Sometimes it wouldn't, but 9 times out of 10 it will. I need this to work with any object, not just a database handler.

Is there a way to tell if the last method I call is the last one in the method itself, so it would perform() without me telling it to?

If not, I might just try to use some keywords like ->now() instead of ->perform()

I have a few idea(s):

  • each method has a parameter that if set will let it know it's at the end (maybe it can loop through all parameters to find if a special constant was passed?) eg $object->do()->something(now);
  • 写回答

3条回答 默认 最新

  • dongmangji0950 2013-10-09 03:14
    关注

    Using an operator overloading extension, you can specify one of the unary operators for prompting executions

    Example:

    ~$db->select()->from()->where();
    

    If you want to avoid installing an extension, one suggestion I can make is using some kind of escape character in your latest chain call to promp the execution. You should omit it from the final query to avoid syntax errors.

    Example:

    $db->select()->from()->where("id = 1 ~"); //~ character implies automatic execution
    
    评论

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源