Should an array or a class be used for holding multiple variables in PHP? -


currently, creating script parse information out of type of report passed it. there part of script pull students information report , save later processing.

is best hold in form of class, or in array variable? figure there 3 methods use.

edit: really, question comes down 1 way have sort of performance advantage? because otherwise, each method same last.

as class direct access variables:

class studentinformation {      public $studentid;     public $firstname;     public $lastname;     public $middlename;     public $programyear;     public $timegenerated;      function studentinformation(){}  } 

as class functions:

class studentinformation {      private $studentid;     private $firstname;     private $lastname;     private $middlename;     private $programyear;     private $timegenerated;      function studentinformation(){}      public function setstudentid($id)     {         $this->studentid = $id;     }      public function getstudentid()     {         return $this->studentid;     }      public function setfirstname($fn)     {         $this->firstname = $fn;     }      /* etc, etc, etc */ } 

or array strings keys:

$studentinfo = array(); $studentinfo["idnumber"] = $whatever; $studentinfo["firstname"] = $whatever; $studentinfo["lastname"] = $whatever; /* etc, etc, etc */ 

  1. trying optimize use of array vs. simple value object unnecessary micro-optimization. simplest cases, array faster because don't have overhead of constructing new object.

  2. it's important remember this: array not data structure exists. if don't need hash capabilities, simple splfixedarraydocs result in lower memory overhead , faster iteration once past initial overhead of object creation. if you're storing large amount of data aforementioned fixed array or 1 of other spl data structures better option.

  3. finally: value objects should immutable, in case recommend encapsulation afforded object on ability assign hash map values willy-nilly. if want simplicity of using array notation, have class implement arrayaccessdocs , best of both worlds. suggest magic getters , setters __get , __set. not, magic obfuscates code unnecessarily. if really need magic, might reconsider design.

there's reason why oop paradigm recognized best programming paradigm we've come -- because it's best paradigm we've come with. should use it. avoid falling trap of many/most php devs use arrays everything.


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -