Ich versuche, eine Antwort JSON-Server in PHP zu bauen. Während der Server das Skript ausführt, verwende ich ein Array in meiner Ausgabe, um Fehler und Erfolge im Skript zu verfolgen. Wie im folgenden Code.PHP set Klassenmitglied durch Referenz
<?php
$output = array();
$output["success"] = array();
$output["error"] = array();
public function foo(){
global $output;
$db = database::getInstance(); //initialize singleton instance
$db->setOutput($output); //set Output log
$db->login(); //log user in
$output["success"][] = "method end"; //debug breakpoint
}
echo json_encode($output, JSON_HEX_QUOT | JSON_HEX_TAG);
Und die Datenbank-Klasse sieht ungefähr so aus ...
<?php
namespace db;
class database{
private var $output_log;
public function setOutputLog(&$output){
$this->output_log = $output;
}
public function login(){
...
$this->output_log["error"][] = "login error";
...
}
}
Und wenn ich das Skript die Ausgabe so immer etwas ...
{
"success" : ["method end"],
"error" : []
}
Die Problem scheint zu sein, wenn ich versuche $ Ausgabe durch Verweis auf die Singleton-Instanz der Datenbankklasse oder im Aufruf von json_encode() zu übergeben. Ich habe überall gesucht und dies ist die einzige Möglichkeit, durch Verweis zu übergeben, aber die lustige Sache ist, wenn es einen Fehler in der Datenbank gibt JSON.parse() im Javascript löst den Fehler, und die Ausgabe ist kein PHP-Fehler Log. Wie auch immer, einen Weg um dies zu wissen, indem Sie die Referenz von $output
übergeben, weil es Zeiten geben wird, wenn ich erwarten $ Ausgabe wird ziemlich groß und ich möchte die Kopien des Arrays auf ein Minimum zu halten und ich bin wirklich nicht gut mit PHP-Array Funktionen plus, wenn ich so etwas getan habe. $ output ["Fehler"] [] = $ db-> login(); und geänderte Login-Methode, um die $output_log
zurückzukehren, würde ich damit enden.
{
"success" : ["method end"],
"error" : ["error":["login error"]]
}
EDIT: Die Änderungen ich Basis, auf Ihre Antworten falls jemand braucht. Ich fuhr fort, eine Antwort Namespace mit zwei Klassen zu machen, die mein Leben so viel einfacher werden.
Response.php
namespace response;
class json_builder
{
private static $instance;
private $output;
private function __construct(){
$this->output = array();
}
public static function getInstance(){
if(is_null(self::$instance)){
self::$instance = new self();
}
return self::$instance;
}
public function addLine($tag, $msg){
$this->output[$tag] = $msg;
}
public function remove($tag){
unset($this->output[$tag]);
}
public function addArray($tag){
$this->output[$tag] = array();
}
public function addArrayLine($tag, $msg){
array_push($this->output[$tag], $msg);
}
public function export($filter){
echo json_encode($this->output, $filter);
unset($this->output);
}
}
class xml_builder
{
private static $instance;
private $output;
private function __construct(){
$this->output = "<?xml version=\"1.0\"?>";
}
public static function getInstance(){
if(is_null(self::$instance)){
self::$instance = new self();
}
return self::$instance;
}
public function addNode($tag, $msg, $attributes){
$this->output += ($attributes) ? "<"+$tag+" "+$attributes+">" : "<"+$tag+">";
$this->output += $msg + "</"+$tag+">";
}
public function startNode($tag, $msg, $attributes){
$this->output += ($attributes) ? "<"+$tag+" "+$attributes+">" : "<"+$tag+">";
}
public function endNode($tag){
$this->output += "</"+$tag+">";
}
public function addInput($msg){
$this->output += $msg;
}
public function export(){
echo $this->output;
unset($this->output);
}
}
und der neue Code aus der Zeit vor wie dieser
valid.php aussehen würde -> Ausgangspunkt
<?php
require_once "response.php";
require_once "db.php";
$output = /response/json_builder::getInstance();
$output->addArray("success");
$output->addArray"error");
public function foo(){
$output = /response/json_builder::getInstance();
$db = database::getInstance(); //initialize singleton instance
$db->login(); //log user in
$output->addArrayLine("success", "method end"); //debug breakpoint
}
$output->export(JSON_HEX_QUOT | JSON_HEX_TAG);
db.php -> Datenbank-Klasse
<?php
namespace db;
class database{
private $xml_output;
private $json_output;
private function __construct()
{
$this->json_output = \response\json_builder::getInstance();
$this->xml_output = \response\xml_builder::getInstance();
}
public static function getInstance()
{
if(is_null(self::$instance))
{
self::$instance = new self();
}
return self::$instance;
}
public function login(){
...
$this->output->addArrayLine("error", "login error");
...
}
}
Danke für die Hilfe Wizard und Chris. =)
Ja, ich finde gerade heraus und es funktioniert lol, aber ich habe jetzt ein neues Problem Ich kann herausfinden, wie man richtig auf Werte zu meinen Arrays innerhalb meiner Arrays. '$ array [" name "] [] =" text "' scheint nur den ersten Eintrag im Array zu überschreiben und das gleiche passiert mit 'array_push ($ array [" name "]," text ")' too. Also denke ich, dass 'JSON.parse()' oder 'json_encode()' das verursacht. –
Gelöst, ich habe eine Variable falsch in meinem XMLHttpRequest, –