How to use getter in PHP like in Android OOP?

I am currently developing an application with PHP using backendless API. I need to get certain data from the table in backendless and store it into an arrays. In Android, I could achieve this using getter. But in PHP, I am confused because I am still new to it.

So this is my code which I try to get the data:

<?php namespace Hospinet; use backendless\Backendless; use backendless\services\persistence\BackendlessDataQuery; use backendless\model\BackendlesCollection; use Hospinet\Pasien; include "backendless/autoload.php"; include "pasien.php"; Backendless::initApp('appid', 'REST key', 'v1'); $Pasien = new Pasien(); $result_collection = Backendless::$Persistence->of("Pasien")->find($data_query_or_relation_depth = null); echo $result_collection->Id_Pasien; //Is this right ? ?>

This is my Pasien.php:

<?php namespace Hospinet; class Pasien { private $Tgl_Lahir; private $Tgl_Masuk; private $Perawatan; private $No_Kamar; private $Nama_Pasien; private $No_Asuransi; private $Jenis_Asuransi; private $Jenis_Kamar; private $Id_RS; private $Id_Dokter; private $Id_Pasien; private $Gender; public function construct( $Tgl_Lahir, $Tgl_Masuk, $Perawatan, $No_Kamar, $Nama_Pasien, $No_Asuransi, $Jenis_Asuransi, $Jenis_Kamar, $Id_RS, $Id_Dokter, $Id_Pasien, $Gender ) { $this->Tgl_Lahir = $Tgl_Lahir; $this->Tgl_Masuk = $Tgl_Masuk; $this->Perawatan = $Perawatan; $this->No_Kamar = $No_Kamar; $this->Nama_Pasien = $Nama_Pasien; $this->No_Asuransi = $No_Asuransi; $this->Jenis_Asuransi = $Jenis_Asuransi; $this->Jenis_Kamar = $Jenis_Kamar; $this->Id_RS = $Id_RS; $this->Id_Dokter = $Id_Dokter; $this->Id_Pasien = $Id_Pasien; $this->Gender = $Gender; } public function getTgl_Lahir() { return $this->Tgl_Lahir; } public function setTgl_Lahir( $Tgl_Lahir ) { $this->Tgl_Lahir = $Tgl_Lahir; } public function getTgl_Masuk() { return $this->Tgl_Masuk; } public function setTgl_Masuk( $Tgl_Masuk ) { $this->Tgl_Masuk = $Tgl_Masuk; } public function getPerawatan() { return $this->Perawatan; } public function setPerawatan( $Perawatan ) { $this->Perawatan = $Perawatan; } public function getNo_Kamar() { return $this->No_Kamar; } public function setNo_Kamar( $No_Kamar ) { $this->No_Kamar = $No_Kamar; } public function getNama_Pasien() { return $this->Nama_Pasien; } public function setNama_Pasien( $Nama_Pasien ) { $this->Nama_Pasien = $Nama_Pasien; } public function getNo_Asuransi() { return $this->No_Asuransi; } public function setNo_Asuransi( $No_Asuransi ) { $this->No_Asuransi = $No_Asuransi; } public function getJenis_Asuransi() { return $this->Jenis_Asuransi; } public function setJenis_Asuransi( $Jenis_Asuransi ) { $this->Jenis_Asuransi = $Jenis_Asuransi; } public function getJenis_Kamar() { return $this->Jenis_Kamar; } public function setJenis_Kamar( $Jenis_Kamar ) { $this->Jenis_Kamar = $Jenis_Kamar; } public function getId_RS() { return $this->Id_RS; } public function setId_RS( $Id_RS ) { $this->Id_RS = $Id_RS; } public function getId_Dokter() { return $this->Id_Dokter; } public function setId_Dokter( $Id_Dokter ) { $this->Id_Dokter = $Id_Dokter; } public function getId_Pasien() { return $this->Id_Pasien; } public function setId_Pasien( $Id_Pasien ) { $this->Id_Pasien = $Id_Pasien; } public function getGender() { return $this->Gender; } public function setGender( $Gender ) { $this->Gender = $Gender; } } Could you please explain it step-by-step how to get the data then store it into an array? Thanks

Hi Proris,

I believe it should be like this:

$result_collection = Backendless::$Persistence->of("Pasien")->find($data_query_or_relation_depth = null);
echo $result_collection->data; 

where data is now a PHP array containing the first 10 (page size) results.

I tried this

$result_collection = Backendless::$Persistence->of(“Pasien”)->find($data_query_or_relation_depth = null)->getAsObject();
echo $result_collection->data;

And I get this result : Notice: Trying to get property of non-object

I already tried to use getAsArray() but it also doesnt work

I tried this

$result_collection = Backendless::$Persistence->of(“Pasien”)->find($data_query_or_relation_depth = null)->getAsObject();
echo $result_collection->data;

And I get this result : Notice: Trying to get property of non-object

I already tried to use getAsArray() but it also doesnt work

Sorry for the confusion, the ->data is not a correct method.

I just ran some tests, you can use $result_collection->getAsArray() to retrieve the results. Probably you were receiving an error with this method because you cannot use echo to print the array. You should use print_r function:

$result_collection = Backendless::$Persistence->of("Pasien")->find($data_query_or_relation_depth = null);
print_r( $result_collection->getAsArray() );

Thanks, it worked. But what I actually asked is how I could use for example the function getId_Pasien in Pasien.php to only get the Id_Pasien column from backendless (Where in Pasien.php there are SETTER and GETTER for each column name)

Well, it appears there’s a lot of issues with this. The responses can only be cast to your custom objects when the following is true:

    You add Backendless::mapTableToClass( 'Pasier', Pasier::class ) Your class is not in a namespace It will only work for findFirst, findLast, findById.
So in your case there's nothing I can advice more since your case does not fit 2 and 3. If you like (and we'd appreciate that), you can contribute to [url=https://github.com/Backendless/PHP-SDK]our PHP SDK on GitHub[/url] and add the functionality you need - this may be not too hard, it's just some mapping are missing inside and the namespace is not handled properly. Unfortunately we don't have a PHP engineer in our team so our support for PHP is limited.

This is what I got:

backendless\model\BackendlessCollection Object 
( 
    [data] => Array 
    ( 
        [offset] => 0 
        [data] => Array 
        ( 
            [0] => Array 
            ( 
                [Id_Dokter] => C0E0607E 
                [created] => 1495593955000 
                [Gender] => L 
                [ownerId] => [Id_Pasien] => 1 
                [No_Asuransi] => H259030 
                [__meta] => {"relationRemovalIds":{},"selectedProperties":["Id_Dokter","created","Gender","ownerId","Id_Pasien","No_Asuransi","Nama_Pasien","No_Kamar","Tgl_Lahir","Perawatan","Jenis_Asuransi","Jenis_Kamar","Id_RS","___class","Tgl_Masuk","updated","objectId"],"relatedObjects":{}} 
                [Nama_Pasien] => Agustinus Kuncoro 
                [No_Kamar] => ICU-2 
                [Tgl_Lahir] => 29 Maret 1979 
                [Perawatan] => Hijau 
                [Jenis_Asuransi] => BPJS 
                [Jenis_Kamar] => ICU
                [Id_RS] => 80923920 
                [___class] => Pasien 
                [Tgl_Masuk] => 17 Maret 2017 
                [updated] => 1495594474000 
                [objectId] => 347D197A-7235-04AB-FF0A-F9D5215F2F00 
             )
         ) 
         [nextPage] => [totalObjects] => 1 
    ) 
    [stored_next_page_link] => 
) 

The question is I can’t print the data separately, such as “Agustinus Kuncoro” from Nama_Pasien.

In the object you got back, you need to get the “data” element (which is an array) and then get the first element of the array (it will be an object). The “Nama_Pasien” property of that object contains the “Agustinus Kuncoro” value

I had the same issue, but i have found a good solution ;
$yourObject = new yourObject();

$listado= $result_collection->getAsArray() ;
$yourObject->setNamaPasien($listado[“0”][“Nama_Pasien”]);

echo( $yourObject->getNamaPasien());