class - Getting protected information from an object in PHP -


i'm pretty new php , trying understand how access properties of objects.

i have class called dog , in it's class have following:

class dog extends resource {     protected $_data = array(         'dog_id' => array(              'type' => resource::property_type_integer,               'value' => null          ),           'owner_id' => array(              'type' => resource::property_type_integer,               'value' => null          )             ) } 

now i'm trying use data in controller.

$drm = dogresoucemodel::create();  $fido = array_values($drm->find($dog_id))[0];  

this works , desired dog object $fido. how owner_id? using xdebug in eclipse can see values attached $fido, can't seem figure out how them. i've tried things along lines of:

$owner_id = $fido["_data"]["owner_id"];   $owner_id = $fido->_data->owner_id;  

the common way implement getter/setter methods:

class dog extends resource {     protected $data = [];//initialize before (no need underscore)      public function getownerid()     {         return $this->data['owner_id']['value'];     }     //setter example     public function setownerid($id)     {         if (!is_numeric($id)) {             throw new invalidargumentexception('owner id numeric');         }         $this->data['owner_id']['value'] = (int) $id;          return $this;     } } 

the rational behind getters , setters allow validate data, normalize , define, in greater levels of detail, how object should behave if, example, value isn't available (ie: if owner_id null, might want throw exception in cases).

the fact can see them in xdebug session because data set, , xdebug allows inspect state of objects. doesn't follow rules of inheritance in can see values in play, regardless of them being private, protected or public. xdebug php extension, it's written in c , sits on top of zend engine (the php internals), not bound syntax, grammar or whatever else part of php.

think of xdebug mri scanner or x-ray machine: allows see going on underneath skin. it's tool doctors use, debuggers tools developers use diagnose problems.

looking @ you've tried, if $data were public, neither approach would've worked:

$owner_id = $fido["_data"]["owner_id"];   

the problem here plies $fido array, or object implements arrayaccess interface. don't know resource looks like, maybe (more on later).

$owner_id = $fido->_data->owner_id;  

this closer if _data public, if value object, (which isn't, it's array).

so should write if $_data public? this:

$ownerid = $fido->_data['owner_id']['value'];//to value $owneridarray = $fido->_data['owner_id'];//will array 

if resource implements arrayaccess interface, chances relies on $_data property, , returns values each key. in case might able write:

$ownerid = $fido['owner_id'] 

or iterate on object:

foreach ($fido $propery => $value) {     //do stuff } 

check php documentation details on the arrayaccess interface


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -