SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    936
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Arrow Why using getter is advisable than not using it?

    Why accessing the variables inside a class
    is not a good programming practice like this below.

    $myobject->$name;

    rather they advise to use getter method, like this below.

    $myobject->get_name();

    can someone please tell to me why?

    and can you give example for this scenario?

    Thank you very much in advance.

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    5 Thread(s)
    The object loses total control over its own state. By using methods to set those properties the object retains control of its state while indirectly allowing others to change it. In your example above name could only be a string how would you enforce that by making name a public property? However, if you use a method to set that property you could easily throw a exception or not set the name if it isn't a string or didn't satisfy other requirements that the property should have. A object should always be in control of its own state and using private or protected properties supports that notion.

  3. #3
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    936
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    thanks.

  4. #4
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by solidcodes View Post
    Why accessing the variables inside a class
    is not a good programming practice like this below.

    $myobject->$name;

    rather they advise to use getter method, like this below.

    $myobject->get_name();

    can someone please tell to me why?

    and can you give example for this scenario?

    Thank you very much in advance.
    First of all you probably mean
    $myobject->name;
    and not
    $myobject->$name;

    The answer is that if you access your variable like this:
    $myobject->name; that would mean that your variable is public and that means that you could accidentally do this:
    $myobject->name = 'something else';

    That would change the value of your variable.

    To prevent this from happening you make your variable private or protected, so it cannot be changed from outside the object.

    Then once you made your variable private or protected, the only way to get its value if through the getter method.

  5. #5
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    On top of what oddz said, if you had a value that was accessible from outside scripts, but it had to be generated on the fly (perhaps for performance reasons), you would have inconsistency in your code because you would call a method for some things and not for others.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •