PHP与大多数面向对象编程语言一样,不支持多重继承.也就是说每个类只能继承一个父类.为了解决这个问题,PHP引入了接口,接口的思想是指定了一个实现了该接口的类必须实现的一系列方法.接口是一种特殊的抽象类,抽象类又是一种特殊的类,所以接口也是一种特殊的类,为什么说接口是一种特殊的抽象类呢?如果一个抽象类里面的所有的方法都是抽象方法,那么我们就换一种声明方法使用"接口";也就是说接口里面所有的方法必须都是声明为抽象方法,另外接口里面不能声明变量(但可声明常量constant),而且接口里面所有的成员都是public权限的.所以子类在实现的时候也一定要使用public权限实限.
声明一个类的时候我们使用的关键字是"class",而接口一种特殊的类,使用的关键字是"interface";
类的定义: class 类名{ ... } ,接口的声明:interface 接口名{ ...}
代码
php
//定义一个接口使用interface关键字,"One"为接口名称
interface One
{
//定义一个常量
const constant = 'constant value';
//定义了一个抽象方法"fun1"
public function fun1();
}
因为接口是一种特殊的抽象类,里面所有的方法都是抽象方法,所以接口也不能产生实例对象; 它也做为一种规范,所有抽象方法需要子类去实现.
我们可以使用"extends"关键字让一个接口去继承另一个接口:
//使用"extends"继承另外一个接口
interface Two extends One
而我们定义一接口的子类去实现接口中全部抽象方法使用的关键字是"implements",而不是我们前面所说的"extends";
//使用"implements"这个关键字去实现接口中的抽象方法 接口和类之间
class Three implements One
function fun1()
...
//实现了全部方法,我们去可以使用子类去实例化对象了
$three=new Three();
我们也可以使用抽象类,去实现接口中的部分抽象方法,但要想实例化对象,这个抽象类还要有子类把它所有的抽象方法都实现才行;
在前面我们说过,PHP是单继承的,一个类只能有一父类,但是一个类可以实现多个接口,就相当于一个类要遵守多个规范,就像我们不仅要遵守国家的法律,如果是在学校的话,还要遵守学校的校规一样;
//使用implements实现多个接口
class Four implemtns 接口一, 接口二, ....
//必须把所有接口中的方法都要实现才可以实例化对象.
PHP中不仅一个类可以实现多个接口,也可以在继承一个类的同时实现多个接口, 一定要先继承类再去实现接口;
//使用extends继承一个类,使用implements实现多个接口
class Four extends 类名一 implemtns 接口一, 接口二, ....
//所有接口中的方法都要实现才可以实例化对象
.........
这个东西有点泛.
我们可以先看看APP接口都需要实现什么功能
①. APP应用需要获取新闻列表信息,展示到APP里面
一般接口交互都用什么形式呢?
根据需求,或者说根据自己team的熟练方面,用哪种进行选取.
怎么做接口呢?
比如是新闻的列表数据
可以放在数据套数组里面
外层数组就是把这些一个一个内层数组包进去.
然后用PHP的数组 json_decode 进行编码,就会变成一个JSON格式的字符串, 只要把这个接口给APP请求,就可以获取了
然后APP再进行解析填充到里面 就行了
首先你要写一个接口文档,定义数据结构
然后开始封装写类
class a{
public function(){
$a = $_GET['a'];
echo '这里面写业务逻辑';
我自己封装的一个
class AppConfig{
public static $dbParam = array(
'dbHost' = 'localhost',
'dbUser' = 'root',
'dbPassword' ='',
'dbName' = '数据库名',
'dbPrefix' = 'test_',
'dbPconnect' = 0,
'dbDebug' = true,
);
class Model {
private $version = ''; //mysql版本
private $config = array(); //数据库配置数组
private $class; //当前类名
public $tablepre = 'ts_'; //表前缀
public $db = ''; //库名
public $table = ''; //表名
private static $link; //数据库链接句柄
private $data = array(); //中间数据容器
private $condition = ''; //查询条件
private $fields = array(); //字段信息
private $sql = array(); //sql集合,调试用
public $primaryKey = 'id'; //表主键
//构造函数初始化
public function __construct($dbParam = array()) {
$this-config = (is_array($dbParam) !empty($dbParam)) ? $dbParam : AppConfig::$dbParam;
$this-connect();
$this-init();
//链接数据库
private function connect() {
if($this-config['dbPconnect']) {
self::$link = @mysql_pconnect($this-config['dbHost'], $this-config['dbUser'], $this-config['dbPassword']);
}else{
self::$link = @mysql_connect($this-config['dbHost'], $this-config['dbUser'], $this-config['dbPassword'], true);
mysql_errno(self::$link) != 0 $this-errdie('Could not connect Mysql: ');
$this-db= !empty($this-db) ? $this-db : $this-config['dbName'];
$serverinfo = $this-version();
mysql_query("SET character_set_connection=".$this-config['dbCharset'].",character_set_results=".$this-config['dbCharset'].",character_set_client=binary", self::$link);
mysql_query("SET sql_mode=''", self::$link);
@mysql_select_db($this-db, self::$link) or $this-errdie('Cannot use database');
return self::$link;
//表基本信息初始化
protected function init() {
$this-class = get_class($this);
$this-table = !empty($this-table) ? $this-table : strtolower($this-class);
$this-table = $this-tablepre . $this-table;
return $this;
//设置属性值
public function __set($name, $value) {
//exit($value);
$this-data['fields'][$name] = $value;
//获取属性值
public function __get($name) {
if(isset($this-data['fields'][$name])) {
return($this-data['fields'][$name]);
}else {
return NULL;
//字段信息处理
private function implodefields($data) {
if (!is_array($data)) {
$data = array();
$this-fields = !empty($this-data['fields']) ? array_merge($this-data['fields'], $data) : $data;
foreach($this-fields as $key = $value) {
$fieldsNameValueStr[] = "+$key+='$value'";
$fieldsNameStr[] = "+$key+";
$fieldsValueStr[] = "'$value'";
return array($fieldsNameValueStr, $fieldsNameStr, $fieldsValueStr);
//条件判断组装
private function condition($where = NULL) {
if (is_numeric($where)) {
$where = "WHERE +{$this-primaryKey}+='{$where}' LIMIT 1";
}elseif (is_array($where)){
$where = "WHERE +{$this-primaryKey}+ in (".implode(',',$where).")";
}elseif(!empty($this-data['condition'])){
//'预留WHERE', 'order', 'group', 'limit' ............等条件关键词处理接口
$where = $where ? "WHERE {$where}" : "WHERE 1";
isset($this-data['condition']['where']) $where .= ' AND '.$this-data['condition']['where'];
isset($this-data['condition']['group']) $where .= ' GROUP BY '.$this-data['condition']['group'];
isset($this-data['condition']['order']) $where .= ' ORDER BY '.$this-data['condition']['order'];
isset($this-data['condition']['limit']) $where .= ' LIMIT '.$this-data['condition']['limit'];
$where = "WHERE {$where}";
$this-condition = $where;
//插入数据
public function insert($data = array(), $replace = false) {
$fields = $this-implodefields($data);
$insert = $replace ? 'REPLACE' : 'INSERT';
$this-query($sql);
return $this-getInsertId();
//更新数据
public function update($data = array() ,$where = '') {
$numargs = func_num_args();
if ($numargs == 1) {
$where = $data;
$this-condition($where);
$sql = "UPDATE +{$this-db}+.+{$this-table}+ SET ".implode(', ',$fields[0])." {$this-condition}";
return $this-getAffectedRows();
//删除数据
public function delete($where = NULL) {
$sql = $where;
$sql = "DELETE FROM +{$this-db}+.+{$this-table}+ {$this-condition}";
//查询数据
public function select($where = NULL, $fields = '*') {
$sql = "SELECT {$fields} FROM +{$this-db}+.+{$this-table}+ {$this-condition}";
return $this-fetch($this-query($sql));
//查询一条数据
public function getOne($where, $fields = '*') {
$data = $this-select($where, $fields = '*');
if($data) {
return $data[0];
return array();
//查询多条数据
public function getAll($where, $fields = '*') {
return $data;
//结果数量
public function getCount($where = '', $fields = '*') {
$sql = "SELECT count({$fields}) as count FROM +{$this-db}+.+{$this-table}+ {$this-condition}";
$data = $this-query($sql);
if($data){
return @mysql_result($data,0);
return 0;
public function query($sql, $flag = '0', $type = '') {
if ($this-config['dbDebug']) {
$startime = $this-microtime_float();
//查询
if ($type == 'UNBUFFERED' function_exists('mysql_unbuffered_query')) {
$result = @mysql_unbuffered_query($sql, self::$link);
} else {
//exit($sql);
$result = @mysql_query($sql, self::$link);
//重试
$result = $this-query($sql);
if ($result === false) {
$this-errdie($sql);
$endtime = $this-microtime_float();
$this-sql[] = array($sql,$endtime-$startime);
//清空操作数据
$this-data = array();
return $flag == '0' ? $result : ($flag == '1' ? $this-getInsertId() : $this-getAffectedRows());
//返回结果$onlyone为true返回一条否则返回所有,$type有MYSQL_ASSOC,MYSQL_NUM,MYSQL_BOTH
public function fetch($result, $onlyone = false, $type = MYSQL_ASSOC) {
if($result){
if ($onlyone) {
$row = @mysql_fetch_array($result, $type);
return $row;
$rowsRs = array();
$rowsRs[] = $row;
return $rowsRs;
//可以运行SELECT,SHOW,EXPLAIN 或 DESCRIBE 等返回一个资源标识符的语句得到返回结果数组
public function show($sql, $onlyone = false) {
return $this-fetch($this-query($sql), $onlyone);
// 使用call函数处理同类型函数
private function __call($name, $arguments) {
$callArr = array('on', 'where', 'order', 'between', 'group', 'limit');
if (in_array($name, $callArr)) {
$this-data['condition'][$name] = $arguments[0];
$this-errdie("function error: function {$name} is not in ($this-class) class exist");
//返回最后一次插入ID
public function getInsertId() {
return @mysql_insert_id(self::$link);
//返回受影响行数
public function getAffectedRows() {
return @mysql_affected_rows(self::$link);
//获取错误信息
private function error() {
return ((self::$link) ? @mysql_error(self::$link) : @mysql_error());
//获取错误信息ID
private function errno() {
return ((self::$link) ? @mysql_errno(self::$link) : @mysql_errno());
//获取版本信息
function version() {
if(empty($this-version)) {
$this-version = mysql_get_server_info(self::$link);
return $this-version;
//打印错误信息
private function errdie($sql = '') {
die('/BRBMySQL ERROR/B/BR
SQL:'.$sql.'/BR
ERRNO:'.$this-errno().'/BR
ERROR:'.$this-error().'/BR');
die('DB ERROR!!!');
//获取时间微妙数
private function microtime_float()
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
//析构函数
public function __destruct() {
echo 'hr';
$this-config['dbDebug'] print_r($this-sql);
//unset($this-result);
//unset($this-condition);
//unset($this-data);
class user extends Model {
//public $db = 'qsf_mvc';
//public $table = 'user';
public $primaryKey = 'uid';
$userObj = new user();
//---------------------------------------插入数据方法一-----------------------------------------
//模拟ActiveRecord模式 插入数据
$userObj-username = 'hoho';
$userObj-sex = 1;
$userObj-desc = '清洁工';
$insetId = $userObj-insert();
if ($insetId 0) {
echo "插入ID为:{$insetId}BR";
//---------------------------------------插入数据方法二-----------------------------------------
//直接数组做参数插入数据
$userArr = array(
'username' = 'hoho',
'sex' = '1',
'desc' = '厨师',
$insetId = $userObj-insert($userArr);
//---------------------------------------更新数据方法一----------------------------------------
$userObj-username = 'h111oho';
if ($affectedRows1 0) {
echo "影响行数为:{$affectedRows1}BR";
//---------------------------------------更新数据方法二----------------------------------------
//更新记录(传递参数的方式和insert操作一样)
'username' = 'hohoho',
'sex' = '0',
'desc' = '厨师qq',
$affectedRows = $userObj-update($userArr, $insetId);
if ($affectedRows 0) {
echo "影响行数为:{$affectedRows}BR";
//----------------------------------------查询数据----------------------------------------------
//print_r($userRs0);
//print_r($userRs1);
//----------------------------------------删除数据-----------------------------------------------
//删除操作传递参数的方式和select操作一样
$userObj-delete('delete from user where uid 100'); //直接完整sql语句
$userObj-delete("+uid+ 100"); //where条件
//----------------------------------------特殊查询-----------------------------------------------
$userShowRs = $userObj-show('show create table user', true); //获取特殊查询的结果,第二个参数代表返回一条结果还是所有的结果
进入php源程序目录中的ext目录中,这里存放着各个扩展模块的源代码,选择你需要的模块,比如curl模块:cd curl
执行phpize生成编译文件,phpize在PHP安装目录的bin目录下
运行时,可能会报错:Cannot find autoconf. Please check your autoconf installation and
the $PHP_AUTOCONF
environment variable is set correctly and then rerun this
script.,需要安装autoconf:
yum install autoconf(RedHat或者CentOS)、apt-get install
autoconf(Ubuntu Linux)
执行这个命令时,php会去检查配置文件是否正确,如果有配置错误,
这里会报错,可以根据错误信息去排查!
.*****.com?token=yanzhengtype=jsondata=ourdata
格式可以这样设计,,token 是咱们首先在php中验证的内容,type是告诉用户的返回的同样是json数据 data 是要接受的数据
$token = $_GET['token']; php页面接受
$data = $_GET['data'] 得到数据后 再在php页面分析数据,最后入库 入库操作就不用说了吧
以上就是土嘎嘎小编为大家整理的数据库通过php创建接口相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!