php - Query array or object property? -


i'm still new oop , simple question, not sure if i'm overthinking this.

let's have simple class following can use instantiate object can generate array:

class gen_arr {     public $arr = array();     public function fill_arr() {      $this->arr["key"] = "value";    }  }  // instantiate object gen_arr $obj = new gen_arr(); 

now if wanted value of object's array's item, generate array first , echo value like:

$arr = $obj->fill_arr();

echo $arr["key"];

or access object's property directly?

echo $obj->arr["key"]

in actual code property private , there method allows viewing of property array, above simplify question.

are there performance considerations and/or best practices when comes kind of case?

update:

it's still unclear answers if best way generate array property , access array or access property directly (through getter method)

first off, class shared has range of problems:

  • its sole instance property public , can modified anyone
  • you have temporal coupling, method fill_arr() needs invoked before accessing the value makes sense

encapsulation

reduce visibility of instance property public private, property can modified object itself, , provide accessor instead:

class gen_arr {     private $arr;      public function fill_arr()      {         $this->arr["key"] = "value";     }      public function arr()     {         return $this->arr;     } } 

temporal coupling

remove method fill_arr() , instead initialize property $arr in 1 of following options:

  • initialize field lazily when accessed first time
  • initialize field in constructor
  • initialize field default value
  • initialize field value injected via constructor

initialize field lazily when accessed first time

initialize field when it's accessed first time:

class gen_arr {     private $arr;      public function arr()     {         if (null === $this->arr) {             $this->arr = [                 'key' => 'value',             ];         }          return $this->arr;     } } 

initialize field in constructor

assign value during construction:

class gen_arr {     private $arr;      public function __construct()     {         $this->arr = [             'key' => 'value',         ];     }      public function arr()     {         return $this->arr;     } } 

initialize field default value

assign value field directly, works fine if don't need computation:

class gen_arr {     private $arr = [         'key' => 'value',     ];      public function arr()     {         return $this->arr;     } } 

initialize field value injected via constructor

if values not hard-coded or otherwise calculated (as in previous examples), , need able instantiate objects different values, inject values via constructor:

class gen_arr {     private $arr;      public function __construct(array $arr)     {         $this->arr = $arr;     }      public function arr()     {         return $this->arr;     } } 

accessing , dereferencing values

this seems actual question, answer - of course - it depends!.

let's assume have provided accessor instead of accessing otherwise public field directly:

since php 5.4, following possible:

$object = new gen_arr();  echo $object->arr()['key']; 

if still using older version of php, can't , have instead:

$object = new gen_arr();  $arr = $object->arr();  echo $arr['key']; 

largely, though, answer question depends on circumstances, , want achieve. after all, readability key maintenance, might make sense introduce explaining variable.

note example, use arrayobject instead:

$arr = new \arrayobject([     'key' => 'value', ]);  echo $arr['key']); 

for reference, see:

for example, see:


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -