Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6164229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:57:30+00:00 2026-05-23T21:57:30+00:00

I’ve been suggested to use Mappers to separate object storage in db from real

  • 0

I’ve been suggested to use Mappers to separate object storage in db from real object behaviour, which sounds like a nice idea. But being used to other OOP languages like Java or SmallTalk, I always try to encapsulate my objects the most I can.
I’ve read several mappers examples and some of them access the variables of the domain object directly (which would be my last choice, though I know it’s more comfortable).

The only solution I currently come up with is something like:

class User{
   public __construct($mapped_data){
   foreach($data as $key=>$value){
      $this->$key=$value;
   }
...
}

class UserMapper{
   private populate($user,$db_data){
      ...
      $map=array('id'=>'id','psw'=>'password','mail'=>'email', ...);
      foreach($db_data as $key=>$value){
         $mapped_data[$map[$key]]=$value;
      }
      return new User($mapped_data);
   }
}

The map is because the class User doesn’t have to know the db column names at all.

Is this approach correct? Is there a better one?
Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T21:57:31+00:00Added an answer on May 23, 2026 at 9:57 pm

    I would suggest that Data Access Objects are a more appropriate pattern for what you want to achieve. The Mapper that you show above just increases the complexity but doesn’t really provide much in the way of benefits.

    In terms of getters and setters, you can use some of PHP’s magic methods to clean up the code above. The following allows you to store your data in a protected array while providing access to it that looks like it’s just via the object property, but is actually through a method (so that you can provide your own logic there; make things read only, or hide some, etc).

    <?php
    class User {
    protected $_data = array();
    public function __construct($data) {
        $this->_data = $data;
    }
    
    // Get any value
    public function __get($key) {
        return array_key_exists($key, $this->_data) ? $this->_data[$key] : null;
    }
    
    // Set any value; leave out to make the data read only outside the class
    public function __set($key, $val) {
        $this->_data[$key] = $val;
    }
    
    // You should also implement __isset() and __unset()
    }
    

    You can then access the data like

    <?php
    $user = new User(array('foo' => 'bar'));
    
    // Outputs "bar"
    echo $user->foo;
    

    You might also simplify this all for yourself by using an existing PHP ORM library.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I have a text area in my form which accepts all possible characters from
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.