gpt4 book ai didi

PHP实现链式操作的核心思想

转载 作者:qq735679552 更新时间:2022-09-27 22:32:09 25 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章PHP实现链式操作的核心思想由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

PHP 链式操作的实现 。

  。

复制代码 代码如下:

 $db->where()->limit()->order();
 

  。

在 Common 下创建 Database.php.

链式操作最核心的地方在于:在方法的最后 return $this,

Database.php

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
namespace Common;
 
class Database{
   function where( $where ){
     return $this //链式方法最核心的地方在于:在每一个方法之后 return $this
   }
   function order( $order ){
     return $this ;
   }
   function limit( $limit ){
     return $this ;
   }
}

index.php

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
define( 'BASEDIR' ,__DIR__); //定义根目录常量
include BASEDIR. '/Common/Loader.php' ;
spl_autoload_register( '\\Common\\Loader::autoload' );
 
$db = new \Common\Database();
 
//传统的操作需要多行代码实现
//$db->where('id = 1');
//$db->where('name = 2');
//$db->order('id desc');
//$db->limit(10);
 
//使用链式操作,一行代码解决问题
$db ->where( 'id = 1' )->where( 'name = 2' )->order( 'id desc' )->limit(10);

在使用链式操作时,ide(netbeans 会给出自动提示):

  。

最后此篇关于PHP实现链式操作的核心思想的文章就讲到这里了,如果你想了解更多关于PHP实现链式操作的核心思想的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com