この PHP Web サービスを作成しようとしています。
ご覧のとおり、クラス「Aluno」には2つのプライベート変数があります。これに一日中苦労した後、これらの変数をパブリックに変更すると、すべてが期待どおりに進むことが最終的にわかりました。しかし、それらが非公開のままである場合、面白いことが起こります。それらの配列は空を返しますが、オブジェクトを使用して、結果を見てください。
なんとか機能させましたが、プライベート変数では機能しない理由がわかりません。
public vars を使用した webservice からの結果:
{
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();