我正在尝试制作这个PHP网络服务。
在“Aluno”课上你可以看到有2个私人变种。 经过一整天的努力,我终于弄清楚,如果我将这些变量公之于众,那么一切都按预期进行。但如果他们保持私密,有趣的事情发生,他们的数组返回空,但与对象,看看结果:
即使我设法让它发挥作用,我也不明白为什么它不适用于私人变种。
带有公共变种的网络服务的结果:
{
alunos: [
{
_nome: "Fexxxx",
_bairro: "Rexxxx"
},
{
_nome: "Fexxxxx",
_bairro: "Broxxxx"
},
{
_nome: "Ferxxxx",
_bairro: "Grxxxx"
},
{
_nome: "Maxxxx",
_bairro: "Chxx"
},
{
_nome: "Roxxxx",
_bairro: "Cixxxx"
}
]
}
来自具有私有变量的webservice的结果:
{
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();
答案 0 :(得分:0)
它希望与私人合作,因为私人变量只能在班级内访问。您需要定义getter方法才能返回私有变量。