Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP WebService,空数组,仅包含私有变量_Php_Arrays_Json_Web Services - Fatal编程技术网

PHP WebService,空数组,仅包含私有变量

PHP WebService,空数组,仅包含私有变量,php,arrays,json,web-services,Php,Arrays,Json,Web Services,我试图使这个PHP Web服务 在“Aluno”类中,您可以看到有两个私有变量。 在为此苦苦挣扎了一整天之后,我终于明白,如果我将这些变量改为公共变量,一切都会按预期进行。但如果它们仍然是私有的,有趣的事情发生了,它们的数组返回为空,但对于对象,请查看结果: 即使我设法让它工作,我不明白为什么它不能与私人VAR一起工作 使用公共变量的webservice的结果: { alunos: [ { _nome: "Fexxxx", _bairro: "Rexxxx" }, { _nome: "Fexxx

我试图使这个PHP Web服务

在“Aluno”类中,您可以看到有两个私有变量。 在为此苦苦挣扎了一整天之后,我终于明白,如果我将这些变量改为公共变量,一切都会按预期进行。但如果它们仍然是私有的,有趣的事情发生了,它们的数组返回为空,但对于对象,请查看结果:

即使我设法让它工作,我不明白为什么它不能与私人VAR一起工作

使用公共变量的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();

它希望使用private,因为只能从类内部访问private变量。您需要定义getter方法才能返回私有变量。

但是我没有直接访问变量,这就是我没有getter的原因。json_encode方法可能正在使用它们,它在buscaAlunos()函数中被调用。那么这不应该起作用吗?我想说我已经尝试过使用getter方法了。仍然无法使用私有变量,就像我说的,我没有在函数外使用变量或构造函数。我试图覆盖私有变量的getter和setter方法(即使我没有使用它们)。仍然无法工作。