澳门京葡网站PHP完结再次来到JSON和XML的类分享

weaker in xxx.php on line,json($data) {,null),复制代码 代码如下,按json方式输出通信数据,@paramstring $type 数据类型,@param $message 描述信息,@param $data 数据

今天先后忽地冒出那样的报错
Fatal error: Access level to xxx must be protected (as in class xxx) or
weaker in xxx.php on line

代码很简短,效用也很轻易实用,这里就不多废话了,直接奉上代码:

//db.php

<?php
Class Response
{

原因是子类中定义了与父类同样的点子名

复制代码 代码如下:


/**
* 返回json数据
* @param $code 状态码
* @param $message 描述音信
* @param $data 数据
* @param $type 类型
* @param $callback 回调函数
**/
public static function show($code,$message=”,$data=”,$type =
‘json’,$callback=”)
{
if($type==’json’)
{
echo self::jsonEncode($code,$message,$data);
}elseif($type == ‘xml’)
{
echo self::xmlEncode($code,$message,$data);
}elseif($type == ‘jsonp’)
{
echo $callback.'(‘.self::jsonEncode($code,$message,$data).’)’;
}

子类:
private function return_json($message,$result=’true’) {
        $data = array();
        $data[‘result’] = $result;
        $data[‘message’] = $message;
        self::echo_json($data);
}

<?php
    class Reponse{
        //private $result =
array(‘code’=null,’message’=null,’data’=>null);
        /**
         * @desc 返回JSON格式
         * @param int $code
         * @param string $message
         * @param array  $data
         * return string
         */
        public static function json($code,$message = null,$data =
array()){
            if(!is_numeric($code)){
                return false;
            }
            $result = array(
                ‘code’=>$code,
                ‘message’=>$message,
                ‘data’=>$data
            );
            return json_encode($result);
            exit;
        }
        /**
         * @desc 再次回到xml格式数据
         * @parma int $code 状态码
         * @param string $message 提示
         * @param array $data 数据
         * return string
         */
         public static function xml($code,$message = ”,$data =
array()){
            if(!is_numeric($code)){
                return false;
            }
            $result = array(
                ‘code’=>$code,
                ‘message’=>$message,
                ‘data’=>$data
            );
            $xml = ”;
            $xml .= “<?xml version=’1.0′ encoding=’UTF-8′?>\n”;
            $xml .= “<root>\n”;
            $xml .= self::xmlEncode($result);
            $xml .= “</root>”;
            header(“Content-Type:text/xml”);
            echo $xml;
         }
         public static function xmlEncode($result){
            $xml = $attr =”;
            foreach($result as $key=>$val){
                if(is_numeric($key)){
                    $attr = “id='{$key}'”;
                    $key = “item{$key}”;
                }
                $xml .= “<{$key} {$attr}>”;
                $xml .= is_array($val) ? self::xmlEncode($val) :
$val;
                $xml .= “</{$key}>\n”;
            }
            return $xml;
         }
    }
    $data = array(
        ‘id’=>1,
        ‘age’=>20,
        ‘username’=>’tim’,
        ‘others’=>array(1,2,3),
    );
    Reponse::xml(200,’success’,$data);
    Reponse::json(200,’success’,$data);

/**

}

private function echo_json($data) {
    if (strtoupper(CHARSET) == ‘GBK’){
        $data =
Language::getUTF8($data);//网站GBK使用编码时,调换为UTF-8,制止json输出汉字难题
    }
    echo json_encode($data);
}

小同伙们方可直接拿去选拔,使用格局在代码的最下方:)

* 单例链接数据库

/**
* 返回json数据
* @param $code 状态码
* @param $message 描述音信
* @param $data 数据
*
**/
public static function jsonEncode($code,$message=”,$data=”)
{
if(!is_numeric($code))
{
return ”;
}

父类:

你也许感兴趣的小说:

  • PHP生成json和xml类型接口数据格式
  • php
    json与xml类别化/反种类化
  • php
    备份数据库代码(生成word,excel,json,xml,sql)
  • php中DOMDocument轻易用法示例代码(XML创设、增多、删除、修改)
  • PHP中simplexml_load_string函数使用验证
  • php实现将数组调换为XML的措施
  • PHP操作XML作为数据库的类
  • PHP JSON 数据解析代码
  • ThinkPHP中使用ajax接收json数据的点子
  • php中JSON的选取与转移
  • php达成xml与json之间的并行转变职能实例

* */

$result = array(
‘code’ => $code,
‘message’ => $message,
‘data’ => $data,
);
header(“Content-type:text/json;chaset=utf-8″);
return json_encode($result);
}
/**
* 返回XML数据
* @param $code 状态码
* @param $message 描述音讯
* @param $data 数据
*
**/
public static function xmlEncode($code,$message,$data=array())
{
if(!is_numeric($code))
{
return ”;
}

/**
 * 返回json状态
 */
protected function return_json($message,$result=’true’) {
    $data = array();
    $data[‘result’] = $result;
    $data[‘message’] = $message;
    self::echo_json($data);
}

classDb {

$result = array(
‘code’ => $code,
‘message’ => $message,
‘data’ => $data,
);

protected function echo_json($data) {
    if (strtoupper(CHARSET) == ‘GBK’){
        $data =
Language::getUTF8($data);//网址GBK使用编码时,调换为UTF-8,幸免json输出汉字难点
    }
    echo json_encode($data);die;
}

static private$_instance;

header(“Content-type:text/xml;chaset=utf-8”);
$xml = “<?xml version=’1.0′ encoding=’utf-8′ ?>\n”;
$xml .= “<root>\n”;
$xml .= self::xmlToEncode($result);
$xml .= “</root>\n”;

解决办法

static private$_connectSource;

return $xml;

把子类中的private 改成
protected,大概幸免方法重名。这里例子中很刚强是平等的形式开始展览了再次的定义,子类删除那三个议程就能够。

private$_dbConfig=array(

}

‘host’=>’127.0.0.1’,

/**
* XML内容循环
* @param $data 数据
*
**/
public static function xmlToEncode($data)
{
if(empty($data))
{
return ”;
}
$xml = $attr = ”;
foreach ($data as $key => $value)
{
if(is_numeric($key))
{
$attr = “id='{$key}’”;
$key = “item”;
}
$xml .= “<{$key} {$attr}>”;
$xml .= is_array($value) ? self::xmlToEncode($value) : $value ;
$xml .= “</{$key}>\n”;
}

‘user’=>’root’,

return $xml;
}
}

‘password’=>”,

//二维数据
$type = isset($_GET[‘type’])? $_GET[‘type’] : ‘json’;
$callback = isset($_GET[‘callback’])? $_GET[‘callback’] : ”;

‘database’=>’video’,

$arr = array(
‘id’=>1,
‘name’=>’jia’,
‘type’=>array(1,2,3)
);
// 状态码 描述音讯 数据 类型 回调函数
Response::show(200,’数据重临成功’,$arr,$type,$callback);

);

private function__construct() {

}

static public functiongetInstance() {

if(!(self::$_instanceinstanceof self)) {

self::$_instance=new self();

}

return self::$_instance;

}

public functionconnect() {

if(!self::$_connectSource) {

self::$_connectSource=
@mysql_connect($this->_dbConfig[‘host’],$this->_dbConfig[‘user’],$this->_dbConfig[‘password’]);

if(!self::$_connectSource) {

throw newException(‘mysql connect error ‘. mysql_error());

//die(‘mysql connect error’ . mysql_error());

}

mysql_select_db($this->_dbConfig[‘database’],self::$_connectSource);

mysql_query(“set names UTF8”,self::$_connectSource);

}

return self::$_connectSource;

}

}

//response.php


澳门京葡网站,classResponse {

constJSON=”json”;

/**

* 按综合措施出口通信数据

*@paraminteger $code 状态码

*@paramstring $message 提醒音讯

*@paramarray $data 数据

*@paramstring $type 数据类型

* return string

*/

public static
functionshow($code,$message=”,$data=array(),$type=self::JSON) {

if(!is_numeric($code)) {

return”;

}

if(in_array($_GET[‘format’],array(‘json’,’xml’,’jsonp’))){

$type=’json’;

}

//    $type = isset($_GET[‘format’]) ? $_GET[‘format’] :
self::JSON;

$result=array(

‘code’=>$code,

‘message’=>$message,

‘data’=>$data,

);

if($type==’json’) {

self::json($code,$message,$data);

exit;

}elseif($type==’array’) {

var_dump($result);

}elseif($type==’xml’) {

self::xmlEncode($code,$message,$data);