我正在尝试制作这个 PHP 网络服务。
如您所见,在“Aluno”类上有2个私有变量。在为此苦苦挣扎了一整天后,我终于发现,如果我将这些变量更改为公开,一切都会按预期进行。但是,如果他们保持私密,有趣的事情发生了,他们的数组返回空但有对象,看看结果:
即使我设法使它工作,我也不明白为什么它不适用于私有变量。
来自具有公共变量的网络服务的结果:
{
alunos: [
{
_nome: "Fexxxx",
_bairro: "Rexxxx"
},
{
_nome: "Fexxxxx",
_bairro: "Broxxxx"
},
{
_nome: "Ferxxxx",
_bairro: "Grxxxx"
},
{
_nome: "Maxxxx",
_bairro: "Chxx"
},
{
_nome: "Roxxxx",
_bairro: "Cixxxx"
}
]
}
来自带有私有变量的 web 服务的结果:
{
alunos: [
{ },
{ },
{ },
{ },
{ }
]
}
这是完整的 index.php
// Helper method to get a string description for an HTTP status code
// From http://www.gen-x-design.com/archives/create-a-rest-api-with-php/
function getStatusCodeMessage($status)
{
$codes = Array(
100 => 'Continue',
101 => 'Switching Protocols',
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found',
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
306 => '(Unused)',
307 => 'Temporary Redirect',
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Timeout',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Requested Range Not Satisfiable',
417 => 'Expectation Failed',
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported'
);
return (isset($codes[$status])) ? $codes[$status] : '';
}
function sendResponse($status = 200, $body = '', $content_type = 'application/json') {
$status_header = 'HTTP/1.1 ' . $status . ' ' . getStatusCodeMessage($status);
header($status_header);
header('Content-type: ' . $content_type);
echo ($body);
}
class Aluno {
private $_nome;
private $_bairro;
function __construct() {
$this->setAtributos(NULL,NULL);
}
public function setAtributos($nome, $bairro) {
$this->_nome = $nome;
$this->_bairro = $bairro;
}
public function __get($name) {
$name = '_'.$name;
if (property_exists($this, $name)) {
return $this->$name;
}
}//end getter
public function __set($name, $value) {
$name = '_'.$name;
if (property_exists($this, $name)) {
$this->$name = $value;
}
return $this;
}//end setter
}//end class Aluno
class Teste1 {
private $db;
private $alunosArray;
function __construct() {
$this->db = new mysqli('localhost', 'xxxx', 'xxxx', 'xxxx');
$this->db->autocommit(FALSE);
$this->alunosArray = array();
}
function __destruct() {
$this->db->close();
}
function buscaAlunos() {
if (isset($_GET["nome"])) {
$nomeParam = '%'.$_GET["nome"].'%';
$stmt = $this->db->prepare('SELECT nome,bairro FROM alunos WHERE nome LIKE ? ORDER BY nome');
$stmt->bind_param("s",$nomeParam);
$stmt->execute();
$stmt->bind_result($nomeAlunoLocal,$bairroLocal);
while ($stmt->fetch()) {
$alu = new Aluno;
$alu->setAtributos(utf8_encode($nomeAlunoLocal),utf8_encode($bairroLocal));
array_push($this->alunosArray, $alu);
}//end while
$stmt->close();
sendResponse(200, json_encode(array('alunos'=>$this->alunosArray)));
return true;
}//end if
sendResponse(400, 'parametro invalido');
return false;
}//end function buscaAlunos()
}//end class Teste1
$api = new Teste1;
$api->buscaAlunos();