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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:40:01+00:00 2026-05-23T10:40:01+00:00

Let us say I have a class that looks like this: class UserModel {

  • 0

Let us say I have a class that looks like this:

class UserModel {
    private $_userData;
    function __construct($user_data){
          $this->_userData = $user_data;
    }
}

Which is called like this:

$user = json_decode('{"name":"Neal","surname":"MyLastName"}'); 
$the_user = new UserModel($user);

I do not want so that every time I want to get the user’s name to have to do $this->_userData->name inside the class.

Is it ok if i set a default value for name and surname, and chreate a __get() function like so:

class UserModel {
    private $_userData;
    private $name = 'default';
    private $surname = 'default';
    function __construct($user_data){
          $this->_userData = $user_data;
    }


    function __get($var){
         if(isset($this->_userData->$var))return $this->_userData->$var; // new storage
         if(isset($this->$var))return $this->$var; // old storage
         return null; // the default (could be a throw new Exception() instead)
    }
}

Here is a demo of what I am trying to do: http://codepad.viper-7.com/cuS9Lx

  • 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-23T10:40:02+00:00Added an answer on May 23, 2026 at 10:40 am

    I’m not sure if that is exactly how you would want to do it. Personally, if I had defaults, I would place them all on the class level, and then retrieve them that way:

    class Foo
    {
         static $bar = 1;
         private $_udata;
         public function __get($a)
         {
              return (isset($this->_udata->$a))?
                        $this->_udata->$a:
                        self::$$a; // NOTE DOUBLE $!
         }
    };
    $f = new Foo(); echo $f->bar; // 1
    

    It has a lower footprint, and it has more of “Resolve to default” feel.

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

Sidebar

Related Questions

Let's say I have a very simple PrototypeJS class that looks like this: var
Let's say we have a class that looks like this: class A { public:
Let's say I have a class that looks something like this: class Foo(Prop1:Int, Prop2:Int,
Let's say I have an object MyObject that looks like this: public class MyObject
Let's say I have a manager that looks something like this: public class CustomerManager
Let's say I have an entity that looks like this: public class Album() {
Let's say I have an interceptor that looks smth like this: public class AuthorizationInterceptor
Let's say I have code that looks like this: public abstract class ResourceDownloaderBaseImpl implements
Let's say I have a class with a member that looks like this: readonly
Let's say I have a class that looks like this: public class CallByAttribute {

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.