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 8956179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:43:54+00:00 2026-06-15T14:43:54+00:00

I am wondering what visibility would give best practises on using data mapping pattern?

  • 0

I am wondering what visibility would give best practises on using data mapping pattern?

private/public/protected

When protected I would need to use the getters() in my save() method on the mapper class.

If private and public I would be able to do $user->id; in the mapper class. What does make most sense?

  • 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-06-15T14:43:55+00:00Added an answer on June 15, 2026 at 2:43 pm

    In my opinion and I often use it this way, properties are protected and my class uses magic methods for getters and setters. The way I do it you are able to do both. The best thing I like about using setter-methods is using fluent interfaces.

    /**
     * Handling non existing getters and setters
     *
     * @param string $name
     * @param array $arguments
     * @throws Exception
     */
    public function __call($name, $arguments)
    {
        $type = substr($name, 0, 3);
    
        // Check if this is a getter or setter
        if (in_array($type, array('get', 'set'))) {
            $property = lcfirst(substr($name, 3));
    
            // Only if the property exists we can go on
            if (property_exists($this, $property)) {
                switch ($type) {
                    case 'get':
                        return $this->$property;
                        break;
    
                    case 'set':
                        $this->$property = $arguments[0];
                        return $this;
                        break;
                }
            } else {
                throw new \Exception('Unknown property "' . $property . '"');
            }
        } else {
            throw new Exception('Unknown method "' . $name . '"');
        }
    }
    
    /**
     * Magic Method for getters
     *
     * @param string $name
     * @return mixed
     */
    public function __get($name)
    {
        $method = 'get' . ucfirst($name);
        return $this->$method();
    }
    
    /**
     * Magic Method for setters
     *
     * @param string $name
     * @param mixed $value
     */
    public function __set($name, $value)
    {
        $method = 'set' . ucfirst($name);
        return $this->$method($value);
    }
    

    Using fluent interfaces looks like this:

    $post->setTitle('New Post')
         ->setBody('This is my answer on stackoverflow.com');
    

    Some thoughts on the single possibilities:

    Private

    Using private properties prevents me from extending my model classes and reusing some functionality.

    Public

    There will be no way in to intercept changes to the variables. Sometimes it is needed to do something on setting.

    Protected

    The best way for me. The properties are not publicly changeable and I force other developers to write getters and setters for their properties which have additional functionality. Also in my opinion this way you prevent others from making mistakes in misusing your model class.

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

Sidebar

Related Questions

Wondering what the best / good way of doing this would be in jQuery.
Wondering what is the best or most popular database client tool. Similar to Microsoft's
wondering if some of you know what javascript framework facebook is using ? Thanks
I'm wondering what people's experiences are with in-memory cache solutions for .net. We need
I need to update all the bindings on my UserControl when its visibility changes
Wondering if anyone has had any experience using retina assets with the NativeControls iPhone
I was wondering how to generate permalinks from the following markup, using python markdown
I am wondering if there is a way to check the visibility of a
I'm wondering what the correct syntax is to traverse the data returned of this
Can this object pool cause visibility problems with multiple threads? I'm specifically wondering about

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.