ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Do you return Arrays or Objects from your Models?

September 01, 2011 6:16am

Subscribe [5]
  • #1 / Sep 01, 2011 6:16am

    n0xie

    1381 posts

    Interesting internal discussion. Curious what people think.

  • #2 / Sep 01, 2011 6:27am

    johnwbaxter

    651 posts

    Usually arrays for me. In general i prefer working with arrays rather than objects for data storage.

    The real question is, which medium uses the least memory for storing the same data and which is quickest to access said data, i think it’s arrays, could be wrong though…

  • #3 / Sep 01, 2011 7:17am

    WanWizard

    4475 posts

    As a Datamapper user, most of my model methods return objects.

    However, for specific operations returning large resultsets, I tend to bypass the ORM logic, use direct $this->db calls, and return the data in an array, since it’s a lot faster and more memory efficient.

  • #4 / Sep 01, 2011 8:02am

    Twisted1919

    500 posts

    Interesting question 😊
    Usually, with CI i use AR and return objects (dunno i believe using $obj->property is faster than $obj[‘property’] when writing)

    I have a question though, and i know most of us fight with this per daily basis.
    The situation is that sometime, i have a very large model method which does allot of calculations and validation etc, and i need to return the result which needs to contain more variables and in this case i return a key=>value pair array containing the result of the model and i parse that array in controller.
    For example

    //in controller method
    $result=$this->model->method();
    if($result['success'])
    {
      if(IS_AJAX)
      {
         //this way i can send a json object to jquery then parse it client side.
         exit( json_encode($result) );
      }
      //prepare some messages here for the user based on the $result 
    }
    
    
    //model
    public function method()
    {
       $result=array('success'=>false);
       if(something)
       {
          $result['something'] = 'anything';
       }
       if(!something_else)
       {
          $result['something_else'] = 'hit the brick wall';
          return $result;
       }
       $result['success']=true;
       return $result;
    }

    I don’t know if you can understand what i mean (the example is not the best i can give), but in case you do understand, makes sense to go this way ?

  • #5 / Sep 01, 2011 12:47pm

    Eric Barnes

    487 posts

    I usually always return arrays much for the same reasons as @audiopleb

  • #6 / Sep 01, 2011 7:37pm

    Josh Holloway

    41 posts

    I tend to use objects for database results and arrays for self set data. This means when browsing the code it’s easier to distinguish the data’s origin. I’m not sure if there’s a significant performance boost using one or the other though…

  • #7 / Sep 03, 2011 6:53pm

    Dennis Rasmussen

    601 posts

    Currently doing the same as Josh Holloway.
    I mean… just think of the total amount of time you’ve spent typing [’‘] instead of ->

    The difference in performance between the two is just so little compared to the difference in code readability.
    The only advantage arrays have is when your column names are fucked up (ie. column-name, column.name or columnæøå) and/or you are using a variable to fetch some data: $db->{$foo} vs $db[$foo]

  • #8 / Sep 03, 2011 7:52pm

    JonoB

    133 posts

    Anything db related is an object for me.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases