今天同事突然问起来php对象中的多个箭头->是怎么使用的
例如$obj->a()->b()-c();
那么类是怎么写的呢?像TP那也 $obj->where()->order()->select()这样的查询语句,然后就写个最基本简单查询数据库例子出来可以参考一下。
首先我们建立一个数据库叫mydata,其中的一个表名是 fa_news
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for fa_news
-- ----------------------------
DROP TABLE IF EXISTS `fa_news`;
CREATE TABLE `fa_news` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`content` text CHARACTER SET utf8 COLLATE utf8_general_ci NULL,
`createtime` int(11) NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = MyISAM AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of fa_news
-- ----------------------------
INSERT INTO `fa_news` VALUES (1, '我是一个标题', '这里尚内容', 1570692387);
INSERT INTO `fa_news` VALUES (2, '我是一个标题1', '这里尚内容1', 1570692387);
INSERT INTO `fa_news` VALUES (3, '我是一个标题2', '这里尚内容2', 1570692387);
INSERT INTO `fa_news` VALUES (4, '我是一个标题3', '这里尚内容3', 1570692387);
INSERT INTO `fa_news` VALUES (5, '我是一个标题4', '这里尚内容4', 1570692387);
SET FOREIGN_KEY_CHECKS = 1;
然后我们建一个PHP文件,例如 db.php 代码如下
<?php
class db
{
public $dbms = 'mysql'; //数据库类型
public $host = 'localhost'; //数据库主机名
public $dbName = 'mydata'; //使用的数据库
public $user = 'root'; //数据库连接用户名
public $pass = 'root'; //对应的密码
public $prefix = 'fa_';//表前缀
public $db;
public $op=[];
public function __construct()
{
$dsn="$this->dbms:host=$this->host;dbname=$this->dbName";
try {
//默认这个不是长连接,如果需要数据库长连接,需要最后加一个参数:array(PDO::ATTR_PERSISTENT => true)
//$this->db = new PDO($dsn, $user, $pass, array(PDO::ATTR_PERSISTENT => true));
$this->db = new PDO($dsn, $this->user, $this->pass); //初始化一个PDO对象
} catch (PDOException $e) {
die("Error!: " . $e->getMessage() . "<br/>");
}
}
public function table($name){
$this->op['tablename']=$this->prefix.$name;
return $this;
}
public function where($where){
$this->op['where']=$where;
return $this;
}
public function order($string){
$this->op['order']=$string;
return $this;
}
public function limit($string){
$this->op['limit']=$string;
return $this;
}
public function getsql(){
$sql="SElECT * from ".$this->op['tablename'];
$sql.=isset($this->op['where'])&&trim($this->op['where'])?" where ".$this->op['where']:"";
$sql.=isset($this->op['order'])&&trim($this->op['order'])?" order by ".$this->op['order']:"";
$sql.=isset($this->op['limit'])&&trim($this->op['limit'])?" limit ".$this->op['limit']:"";
return $sql;
}
public function select(){
$sql=$this->getsql();
$sth=$this->db->prepare($sql);
$sth->execute();
$result = $sth->fetchAll();
return $result;
}
public function find(){
$sql=$this->getsql();
$sth=$this->db->prepare($sql);
$sth->execute();
$result = $sth->fetch(PDO::FETCH_ASSOC);
return $result;
}
}
$obj = new db();
$res=$obj->table('news')->order('id asc')->where('')->limit('')->select();
//where()/order()/limit()不掉用或者参数是空都是可以的
echo "<pre>";
print_r($res);
只有为什么可以用多个-> 那是因为方法 return $this;
这样就都懂了吧。其中 table() where() order() limit() 顺序可以不分先后。
这个只是简单的数据库查询,只写了几个简单的方法。
运行的结果就如下
Array
(
[0] => Array
(
[id] => 1
[0] => 1
[title] => 我是一个标题
[1] => 我是一个标题
[content] => 这里尚内容
[2] => 这里尚内容
[createtime] => 1570692387
[3] => 1570692387
)
[1] => Array
(
[id] => 2
[0] => 2
[title] => 我是一个标题1
[1] => 我是一个标题1
[content] => 这里尚内容1
[2] => 这里尚内容1
[createtime] => 1570692387
[3] => 1570692387
)
[2] => Array
(
[id] => 3
[0] => 3
[title] => 我是一个标题2
[1] => 我是一个标题2
[content] => 这里尚内容2
[2] => 这里尚内容2
[createtime] => 1570692387
[3] => 1570692387
)
[3] => Array
(
[id] => 4
[0] => 4
[title] => 我是一个标题3
[1] => 我是一个标题3
[content] => 这里尚内容3
[2] => 这里尚内容3
[createtime] => 1570692387
[3] => 1570692387
)
[4] => Array
(
[id] => 5
[0] => 5
[title] => 我是一个标题4
[1] => 我是一个标题4
[content] => 这里尚内容4
[2] => 这里尚内容4
[createtime] => 1570692387
[3] => 1570692387
)
)