Database results as objects or arrays?

前端 未结 4 744
礼貌的吻别
礼貌的吻别 2020-12-17 17:48

This question is similar to Mysql results in PHP - arrays or objects? However, my question expands on what has been discussed there.

I\'m trying to decide which form

4条回答
  •  鱼传尺愫
    2020-12-17 18:45

    I think its better to represent all of your datas and its type in form of Model. For both joined and singular objects. Doing so will always omit your problem.

    class Member_Details {
        public $id;    
        public $first_name;
        public $last_name;
    
        public function FullName() {
             return $this -> first_name." ".$this -> last_name;
        }
    }
    
    class Member_Address {
        public $id;
        public $address;
        public $city;
    }
    
    class MemberJoins {
         public $objects = array();
    }
    

    After creating such classes you can configures a JOIN in the following way.

    $obj_details = new Member_Details();
    $obj_address = new Member_Address();
    //Add data to the objects and then
    
    //Then create the join object
    $obj_address_details = new MemberJoins();
    $obj_address_details -> objects = array($obj_details, $obj_address);
    

    These both have a common property id from which its data can be linked.

提交回复
热议问题