doujiangao4229 2012-02-06 17:25
浏览 45
已采纳

如何在PHP中创建嵌套方法?

For example, I've seen third-party applications that have functions like this:

$db->select('columns')->from('table')->where('condition');

That's just an example. How do you create methods like that?

  • 写回答

2条回答 默认 最新

  • douzhuo6931 2012-02-06 17:28
    关注

    To accomplish this, each of the methods should return $this, an instance of the class which contains the methods.

    class MyClass {
    
       public function select($x){
          // do something
          return $this;
       }
    
       public function from($x){
          // do something
          return $this;
       }
    
       public function where($x){
          // do something
          return $this;
       }
    
    }
    

    inside these methods, you generally perform some kind of modification to the state of the object.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办