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

The Archive Base Latest Questions

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

I’ve often seen the following described as the correct way of implementing get/set methods:

  • 0

I’ve often seen the following described as the “correct” way of implementing get/set methods:

public class Foo {
    private var _someVar:SomeClass;

    public function get someVar():SomeClass {
       return _someVar;
    }

    public function set someVar(newValue:SomeClass):void {
      _someVar = newValue;
    }
}

Now, because AS3 is returning always references to Object classes, when we use the “get” method we obtain a reference to our private var => encapsulation is broken.

Even if we don’t have a set method we can modify the privar var !
What is the purpose of setting it as private then?

The only solution that I found to this is to return a clone of “_someVar” in our get method, but I’ve never seen this in any example.
So I think I’m losing something here.

Are you returning a clone object from your getters or you just accepting the break in encapsulation?

EDIT
I understand how set and get methods works, and I understand the benefits of them.
I’m asking for the break of “private” access in our private var when we return it with a getter by reference (if our var is of type Number, String, int, etc AS3 is returning always by value, not reference, so we don’t have problem here).
Maybe is not the encapsulation which is broken, because we can’t set the property without a setter method. But we can modify it !

See this example:

public class Foo {
    private var _someVar:Array; // note that this is a Object (not Number, String, etc)

    public function Foo(){
        _someVar = ['don't touch this!'];
    }

    public function get someVar():SomeClass {
       return _someVar;
    }

    // note that we don't have a setter

}


var f:Foo = new Foo(); 
var a:Array = f.someVar;
trace(a[0]); //  'don't touch this!'
a[0] = 'why not?'; 
trace(f.someVar[0]); // 'why not' 

So, we are changing our private var from outside, and without control, even when we don’t have a setter method.

  • 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-23T05:57:27+00:00Added an answer on May 23, 2026 at 5:57 am

    You are controlling access to the member variable when you use get/set functions. For example, if you want the variable to be “read-only” from the outside, but editable from within the class instance you make a get function so that it can be read from outside but do NOT create a set function. This is different from using a private const, because that must be declared immediately and can never be changed from anywhere.

    Similarly, using these functions can allow you to create side-effects for setting the property. For instance:

    public function set foo(value:*):void{
        _foo = value;
        this.dispatchEvent(new Event("fooSet")); 
        // setting foo alerts interested parties
        // that the value of foo has changed
        // without them having to poll foo.
    }
    

    EDIT : Because you’ve updated the question to be more specific, here’s an update of my own.

    You normally would NOT do that. If you’re trying to protect the variable itself, then you won’t offer access to it directly. Doing that breaks “the law of Demeter”. For your specific example with the array, you might do something like this:

    private var _someArray = [true,false];
    
    function get someArray():Array{
        return _someArray.slice(); // Returns a clone of the original array.
    }
    

    As a different example, using a theoretical complex object…

    private var _someObject:SomeObject;
    
    function get someObject():SomeObject{
        return _someObject; // "Wrong."  It breaks the law of demeter.
    }
    
    ////// instead, you would do this.....
    
    function get getSomeObjectsInt():int{
        return _someObject.foo; // where .foo is an int
    } 
    
    
    ////// or this....
    
    function doStuffWithFooObject():Boolean{
       return _someObject.doSomething(); // where doSomething returns a boolean;
    }
    
    
    ///// or this.....
    
    function performActionOnData(pData:String):String{
        return _someObject.someActionWithString(pData); 
    }
    

    That last one is interesting because you don’t need to expose to the world that you’re using SomeObject to do the work… you’re just advertising that you yourself can do it.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
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

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.