PHP與大多數(shù)面向?qū)ο缶幊陶Z言一樣,不支持多重繼承.也就是說每個類只能繼承一個父類.為了解決這個問題,PHP引入了接口,接口的思想是指定了一個實現(xiàn)了該接口的類必須實現(xiàn)的一系列方法.接口是一種特殊的抽象類,抽象類又是一種特殊的類,所以接口也是一種特殊的類,為什么說接口是一種特殊的抽象類呢?如果一個抽象類里面的所有的方法都是抽象方法,那么我們就換一種聲明方法使用“接口“;也就是說接口里面所有的方法必須都是聲明為抽象方法,另外接口里面不能聲明變量(但可聲明常量constant),而且接口里面所有的成員都是public權(quán)限的。所以子類在實現(xiàn)的時候也一定要使用public權(quán)限實限。
創(chuàng)新互聯(lián)是專業(yè)的普蘭網(wǎng)站建設(shè)公司,普蘭接單;提供成都做網(wǎng)站、網(wǎng)站設(shè)計,網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進行普蘭網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!
聲明一個類的時候我們使用的關(guān)鍵字是”class”,而接口一種特殊的類,使用的關(guān)鍵字是“interface”;
類的定義: class 類名{ … } ,接口的聲明:interface 接口名{ …}
代碼
代碼
?php
//定義一個接口使用interface關(guān)鍵字,“One”為接口名稱
interface One
{
//定義一個常量
const constant = 'constant value';
//定義了一個抽象方法”fun1”
public function fun1();
//定義了抽象方法”fun2”
public function fun2();
}
?
上例中定義了一個接口”one”,里面聲明了兩個抽象方法“fun1”和”fun2”,因為接口里面所有的方法都是抽象方法,所以在聲明抽象方法的時候就不用像抽象類那樣使用”abstract”這個關(guān)鍵字了,默認的已經(jīng)加上這個關(guān)鍵字,另外在接口里邊的”public”這個訪問權(quán)限也可以去掉,因為默認就是public的,因為接口里所有成員都要是公有的,所在對于接口里面的成員我們就不能使用“private”的和”protected”的權(quán)限了,都要用public或是默認的。另外在接口里面我們也聲明了一個常量“constant“, 因為在接口里面不能用變量成員,所以我們要使用const這個關(guān)鍵字聲明。
因為接口是一種特殊的抽象類,里面所有的方法都是抽象方法,所以接口也不能產(chǎn)生實例對象; 它也做為一種規(guī)范,所有抽象方法需要子類去實現(xiàn)。
我們可以使用”extends”關(guān)鍵字讓一個接口去繼承另一個接口:
?php
//使用”extends”繼承另外一個接口
interface Two extends One
{
function fun3();
function fun4();
}
?
而我們定義一接口的子類去實現(xiàn)接口中全部抽象方法使用的關(guān)鍵字是”implements”,而不是我們前面所說的”extends”;
代碼
?php
//使用“implements”這個關(guān)鍵字去實現(xiàn)接口中的抽象方法 接口和類之間
class Three implements One
{
function fun1()
{
…
}
function fun2()
{
…
}
}
//實現(xiàn)了全部方法,我們?nèi)タ梢允褂米宇惾嵗瘜ο罅?/p>
$three=new Three();
?
我們也可以使用抽象類,去實現(xiàn)接口中的部分抽象方法,但要想實例化對象,這個抽象類還要有子類把它所有的抽象方法都實現(xiàn)才行;
在前面我們說過,PHP是單繼承的,一個類只能有一父類,但是一個類可以實現(xiàn)多個接口,就相當于一個類要遵守多個規(guī)范,就像我們不僅要遵守國家的法律,如果是在學(xué)校的話,還要遵守學(xué)校的校規(guī)一樣;
?php
//使用implements實現(xiàn)多個接口
class Four implemtns 接口一, 接口二, ….
{
//必須把所有接口中的方法都要實現(xiàn)才可以實例化對象。
}
?
PHP中不僅一個類可以實現(xiàn)多個接口,也可以在繼承一個類的同時實現(xiàn)多個接口, 一定要先繼承類再去實現(xiàn)接口;
?php
//使用extends繼承一個類,使用implements實現(xiàn)多個接口
class Four extends 類名一 implemtns 接口一, 接口二, ….
{
//所有接口中的方法都要實現(xiàn)才可以實例化對象
………
}
?
這個東西有點泛。
我們可以先看看APP接口都需要實現(xiàn)什么功能
1 APP應(yīng)用需要獲取新聞列表信息,展示到APP里面
2 用戶注冊/登錄驗證
3 支付
一般接口交互都用什么形式呢?
1 XML 2JSON
根據(jù)需求,或者說根據(jù)自己team的熟練方面,用哪種進行選取。
怎么做接口呢?
比如是新聞的列表數(shù)據(jù)
可以放在數(shù)據(jù)套數(shù)組里面
內(nèi)層數(shù)組 存標題、內(nèi)容鏈接、作者、更新時間、小圖片地址
外層數(shù)組就是把這些一個一個內(nèi)層數(shù)組包進去。
然后用PHP的數(shù)組 json_decode 進行編碼,就會變成一個JSON格式的字符串, 只要把這個接口給APP請求,就可以獲取了
然后APP再進行解析填充到里面 就行了
首先你要寫一個接口文檔,定義數(shù)據(jù)結(jié)構(gòu)
然后開始封裝寫類
class a{
public function(){
$a = $_GET['a'];
echo '這里面寫業(yè)務(wù)邏輯';
echo '輸出結(jié)果366u';
}
}
我自己封裝的一個
?php
class AppConfig{
public static $dbParam = array(
'dbHost' = 'localhost',
'dbUser' = 'root',
'dbPassword' ='',
'dbName' = '數(shù)據(jù)庫名',
'dbCharset' = 'utf8',
'dbPort' = 3306,
'dbPrefix' = 'test_',
'dbPconnect' = 0,
'dbDebug' = true,
);
}
class Model {
private $version = ''; //mysql版本
private $config = array(); //數(shù)據(jù)庫配置數(shù)組
private $class; //當前類名
public $tablepre = 'ts_'; //表前綴
public $db = ''; //庫名
public $table = ''; //表名
private static $link; //數(shù)據(jù)庫鏈接句柄
private $data = array(); //中間數(shù)據(jù)容器
private $condition = ''; //查詢條件
private $fields = array(); //字段信息
private $sql = array(); //sql集合,調(diào)試用
public $primaryKey = 'id'; //表主鍵
//構(gòu)造函數(shù)初始化
public function __construct($dbParam = array()) {
$this-config = (is_array($dbParam) !empty($dbParam)) ? $dbParam : AppConfig::$dbParam;
$this-connect();
$this-init();
}
//鏈接數(shù)據(jù)庫
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();
if ($serverinfo '4.1' $this-config['dbCharset']) {
mysql_query("SET character_set_connection=".$this-config['dbCharset'].",character_set_results=".$this-config['dbCharset'].",character_set_client=binary", self::$link);
}
if ($serverinfo '5.0') {
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;
}
//設(shè)置屬性值
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'])){
//'預(yù)留WHERE', 'order', 'group', 'limit' …………等條件關(guān)鍵詞處理接口
$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'];
}else{
$where = "WHERE {$where}";
}
$this-condition = $where;
return $this;
}
//插入數(shù)據(jù)
public function insert($data = array(), $replace = false) {
$fields = $this-implodefields($data);
$insert = $replace ? 'REPLACE' : 'INSERT';
$sql = "{$insert} INTO `{$this-db}`.`{$this-table}` (".implode(', ',$fields[1]).") values (".implode(', ',$fields[2]).")";
$this-query($sql);
return $this-getInsertId();
}
//更新數(shù)據(jù)
public function update($data = array() ,$where = '') {
$numargs = func_num_args();
if ($numargs == 1) {
$where = $data;
$data = array();
}
$fields = $this-implodefields($data);
$this-condition($where);
$sql = "UPDATE `{$this-db}`.`{$this-table}` SET ".implode(', ',$fields[0])." {$this-condition}";
$this-query($sql);
return $this-getAffectedRows();
}
//刪除數(shù)據(jù)
public function delete($where = NULL) {
if(!is_array($where) strtolower(substr(trim($where), 0, 6)) == 'delete'){
$sql = $where;
}else{
$this-condition($where);
$sql = "DELETE FROM `{$this-db}`.`{$this-table}` {$this-condition}";
}
$this-query($sql);
return $this-getAffectedRows();
}
//查詢數(shù)據(jù)
public function select($where = NULL, $fields = '*') {
if(!is_array($where) strtolower(substr(trim($where), 0, 6)) == 'select'){
$sql = $where;
}else{
$this-condition($where);
$sql = "SELECT {$fields} FROM `{$this-db}`.`{$this-table}` {$this-condition}";
}
return $this-fetch($this-query($sql));
}
//查詢一條數(shù)據(jù)
public function getOne($where, $fields = '*') {
$data = $this-select($where, $fields = '*');
if($data) {
return $data[0];
}
return array();
}
//查詢多條數(shù)據(jù)
public function getAll($where, $fields = '*') {
$data = $this-select($where, $fields = '*');
return $data;
}
//結(jié)果數(shù)量
public function getCount($where = '', $fields = '*') {
$this-condition($where);
$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;
}
//執(zhí)行sql語句(flag為0返回mysql_query查詢后的結(jié)果,為1返回lastid,其他返回影響行數(shù),默認為2返回影響行數(shù))
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);
}
//重試
if (in_array(mysql_errno(self::$link), array(2006,2013)) empty($result) $this-config['dbPconnect']==0 !defined('RETRY')) {
define('RETRY',true); @mysql_close(self::$link); sleep(2);
$this-connect();
$result = $this-query($sql);
}
if ($result === false) {
$this-errdie($sql);
}
if ($this-config['dbDebug']) {
$endtime = $this-microtime_float();
$this-sql[] = array($sql,$endtime-$startime);
}
//清空操作數(shù)據(jù)
$this-data = array();
return $flag == '0' ? $result : ($flag == '1' ? $this-getInsertId() : $this-getAffectedRows());
}
//返回結(jié)果$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;
}else{
$rowsRs = array();
while($row=@mysql_fetch_array($result, $type)) {
$rowsRs[] = $row;
}
return $rowsRs;
}
}
return array();
}
//可以運行SELECT,SHOW,EXPLAIN 或 DESCRIBE 等返回一個資源標識符的語句得到返回結(jié)果數(shù)組
public function show($sql, $onlyone = false) {
return $this-fetch($this-query($sql), $onlyone);
}
// 使用call函數(shù)處理同類型函數(shù)
private function __call($name, $arguments) {
$callArr = array('on', 'where', 'order', 'between', 'group', 'limit');
if (in_array($name, $callArr)) {
$this-data['condition'][$name] = $arguments[0];
}else{
$this-errdie("function error: function {$name} is not in ($this-class) class exist");
}
return $this;
}
//返回最后一次插入ID
public function getInsertId() {
return @mysql_insert_id(self::$link);
}
//返回受影響行數(shù)
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 = '') {
if ($this-config['dbDebug']) {
die('/BRBMySQL ERROR/B/BR
SQL:'.$sql.'/BR
ERRNO:'.$this-errno().'/BR
ERROR:'.$this-error().'/BR');
}
die('DB ERROR?。?!');
}
//獲取時間微妙數(shù)
private function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
//析構(gòu)函數(shù)
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();
//---------------------------------------插入數(shù)據(jù)方法一-----------------------------------------
//模擬ActiveRecord模式 插入數(shù)據(jù)
$userObj-username = 'hoho';
$userObj-passwd = '1478522';
$userObj-email = 'qsf.z11@163.com';
$userObj-sex = 1;
$userObj-desc = '清潔工';
$insetId = $userObj-insert();
if ($insetId 0) {
echo "插入ID為:{$insetId}BR";
}
//---------------------------------------插入數(shù)據(jù)方法二-----------------------------------------
//直接數(shù)組做參數(shù)插入數(shù)據(jù)
$userArr = array(
'username' = 'hoho',
'passwd' = '1478522',
'email' = 'qsf.z2121ia@163.com',
'sex' = '1',
'desc' = '廚師',
);
$insetId = $userObj-insert($userArr);
if ($insetId 0) {
echo "插入ID為:{$insetId}BR";
}
//---------------------------------------更新數(shù)據(jù)方法一----------------------------------------
$userObj-username = 'h111oho';
$userObj-passwd = '1478511122';
$userObj-email = 'qsf111ia@163.com';
$userObj-sex = 1;
$userObj-desc = '清潔工';
$affectedRows1 = $userObj-update(89);
if ($affectedRows1 0) {
echo "影響行數(shù)為:{$affectedRows1}BR";
}
//---------------------------------------更新數(shù)據(jù)方法二----------------------------------------
//更新記錄(傳遞參數(shù)的方式和insert操作一樣)
$userArr = array(
'username' = 'hohoho',
'passwd' = '1474rr4448522',
'email' = 'qsf.rrza@165.com',
'sex' = '0',
'desc' = '廚師qq',
);
$affectedRows = $userObj-update($userArr, $insetId);
if ($affectedRows 0) {
echo "影響行數(shù)為:{$affectedRows}BR";
}
//----------------------------------------查詢數(shù)據(jù)----------------------------------------------
$userRs0 = $userObj-select(8); //單個主鍵值
//print_r($userRs0);
$userRs1 = $userObj-select(array(1,5,8)); //多個主鍵值的數(shù)組
//print_r($userRs1);
$userRs2 = $userObj-select('select count(*) as count from user where uid 20'); //直接完整sql語句
//print_r($userRs2);
$userRs3 = $userObj-select("`uid` 0"); //where條件
//print_r($userRs3);
$userRs4 = $userObj-getOne("`uid` 0"); //獲取單條記錄
//print_r($userRs4);
$usersRs5 = $userObj-getAll("`uid` 0"); ////獲取所有記錄
//print_r($usersRs5);
$usersRs6 = $userObj-limit('0,10')-where('uid 100')-order('uid DESC')-group('username')-select();
//print_r($usersRs6);
//----------------------------------------刪除數(shù)據(jù)-----------------------------------------------
//刪除操作傳遞參數(shù)的方式和select操作一樣
$userObj-delete(60); //單個主鍵值
$userObj-delete(array(1,5,8)); //多個主鍵值的數(shù)組
$userObj-delete('delete from user where uid 100'); //直接完整sql語句
$userObj-delete("`uid` 100"); //where條件
$userObj-limit('5')-where('uid 80')-delete();
//----------------------------------------特殊查詢-----------------------------------------------
$userShowRs = $userObj-show('show create table user', true); //獲取特殊查詢的結(jié)果,第二個參數(shù)代表返回一條結(jié)果還是所有的結(jié)果
進入php源程序目錄中的ext目錄中,這里存放著各個擴展模塊的源代碼,選擇你需要的模塊,比如curl模塊:cd curl
執(zhí)行phpize生成編譯文件,phpize在PHP安裝目錄的bin目錄下
/usr/local/php5/bin/phpize
運行時,可能會報錯: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)
/usr/local/php5/bin/php -v
執(zhí)行這個命令時,php會去檢查配置文件是否正確,如果有配置錯誤,
這里會報錯,可以根據(jù)錯誤信息去排查!
.*****.com?token=yanzhengtype=jsondata=ourdata
格式可以這樣設(shè)計,,token 是咱們首先在php中驗證的內(nèi)容,type是告訴用戶的返回的同樣是json數(shù)據(jù) data 是要接受的數(shù)據(jù)
$token = $_GET['token']; php頁面接受
$data = $_GET['data'] 得到數(shù)據(jù)后 再在php頁面分析數(shù)據(jù),最后入庫 入庫操作就不用說了吧
網(wǎng)站名稱:數(shù)據(jù)庫通過php創(chuàng)建接口,php實現(xiàn)接口
瀏覽地址:http://www.muchs.cn/article32/hssgsc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站營銷、企業(yè)建站、網(wǎng)站策劃、外貿(mào)建站、網(wǎng)站改版
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)