Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

PHP WebService returns empty array only with private vars

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

PHP WebService returns empty array only with private vars

Postby philmottinPHP » Fri Nov 30, 2012 10:30 pm

Hello everyone!

Im trying to make this PHP webservice.

on the class "Aluno" as you can see there are 2 private vars. After struggling all day on this I finally figure out that if I change those vars to public, everything goes as expected. But if they remain in private, funny thing happen, the array is returned empty but with the objects, look at the results:

Even though I manage to make it work, I don't understand why It won't work with private vars. I was hoping that overriding the constructors with __get and __set would work... but it doesn't...

So, when dealing with webservices, public vars for my data object ("Aluno" in this case) is the only way to go?

Thank you in advance!!

result from webservice with public vars:
Code: Select all
{
alunos: [
{
_nome: "Fexxxx",
_bairro: "Rexxxx"
},
{
_nome: "Fexxxxx",
_bairro: "Broxxxx"
},
{
_nome: "Ferxxxx",
_bairro: "Grxxxx"
},
{
_nome: "Maxxxx",
_bairro: "Chxx"
},
{
_nome: "Roxxxx",
_bairro: "Cixxxx"
}
]
}


result from webservice with private vars:
Code: Select all
{
alunos: [
{ },
{ },
{ },
{ },
{ }
]
}


here is the full index.php
the code is pretty simple.
at the bottom, there are 2 classes:
Aluno - dataModel
Teste1 - main class has a buscaAlunos() function witch request data from my database(change to your settings if you are gonna test) then build the array and encode it on JSON.
The initial functions are just to set appropriate status and content type.
Code: Select all
// 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();
philmottinPHP
New php-forum User
New php-forum User
 
Posts: 1
Joined: Fri Nov 30, 2012 10:06 pm

Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 5 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.