SitePoint Sponsor

User Tag List

Page 2 of 2 FirstFirst 12
Results 26 to 36 of 36
  1. #26
    SitePoint Evangelist
    Join Date
    Aug 2005
    Location
    Winnipeg
    Posts
    498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Im a Scot. As we know Scotsmen all wear kilts and, for better or worse, there is a great deal of interest in what we wear underneath. Now, if you met me, Im sure youd ask politely using my public ear interface. You wouldnt just hoik up my kilt and have a good old rummage around, leaving me exposed to the inclement Scottish summer.

    I sort of feel the same way about direct property access.
    LOL that was hilarious
    The only constant in software is change itself

  2. #27
    SitePoint Addict SirAdrian's Avatar
    Join Date
    Jul 2005
    Location
    Kelowna, BC
    Posts
    289
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I use getX, setX when dealing with objects with non-dynamic variables. Property is kept protected, so I can have it read-only if necessary (no setX field would be declared). The object has a real interface in this these cases, and all methods would be documented.

    I use __set, __get when dealing with a data object where the underlying data is variable, and really just depends what the data is. Example: dealing with database data. In this case, reading/writing any of the dynamic fields is the interface. I see no point in having getX/setX for every field when defining the interaction once will do.


    Either way, it's not "public" access, and if it's at least magic methods, I can control how they function more than a regular public property.
    Adrian Schneider - Web Developer

  3. #28
    SitePoint Enthusiast
    Join Date
    Oct 2008
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    another point in favor of getX and setX is the autocomplete option in your favorite IDE. you may forget what that darn property name was and a magic method wont hint your dev environment. also, you will get an error if u call a setNon_existant_property() method whereas with __set u wont, or it just wont be worth it.

    i just use public fields wherever possible, leave the validation and logic to another class.

    PHP Code:

    $user 
    = new User();
    $user->username "some_long_invalid_username#$@#%";

    $userModel = new UserModel();
    $registrationResult $userModel->register($user);

    var_dump$registrationResult->errors ); 

  4. #29
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by antirealm View Post
    another point in favor of getX and setX is the autocomplete option in your favorite IDE.
    Strictly speaking, that is a property of the IDE - not the language. You could write a plugin for your IDE, that could autocomplete on __get __set (Provided of course that your IDE is open source).

    More generally, this is the age old static vs. dynamic typing discussion. If you define certain things in static time, then it is easier to reason about the program, without executing it. On the other hand, dynamic features are more flexible and generally less verbose. It's not obvious that one way is better than the other - It really depends on context and preference.

  5. #30
    SitePoint Member
    Join Date
    Sep 2009
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I only use getX and setX when they have logic besides getting and setting variables. Otherwise I make properties public. It ain't Java, it's PHP. The result is 1) faster 2) more readable 3) more expressive.

    Code:
    $money = $wallet->getMoney(); //does this gets money out, or just gets them?
    $money = $wallet->money; //now I know for sure
    
    $wallet->setMoney(functionX($a, functionT($x), $y)); //need to count braces
    $wallet->money = functionX($a, functionT($x), $y); //don't need to count braces

  6. #31
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    The problem with that approach is a matter of consistency.

    If I have an object variable, and I want to set 5 different properties, I want to set them all with the same code.

    In my opinion:
    PHP Code:
    <?php
    //Best:
    $Object->setA($A);
    $Object->setB($B);
    $Object->setC($C);
    $Object->setD($D);
    $Object->setE($E);
    //Alright:
    $Object->$A;
    $Object->$B;
    $Object->$C;
    $Object->$D;
    $Object->$E;
    //Dodgy:
    $Object->$A;
    $Object->setB($B);
    $Object->$C;
    $Object->setD($D);
    $Object->setE($E);
    Sure, it's PHP, not Java. That's a BETTER reason to use getters and setters, because at least in Java you have enforced datatypes - the use of getters and setters allows you to cast/convert them to certain data types - so a string wouldn't be set in a property meant for an integer.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  7. #32
    SitePoint Wizard Ren's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    1,060
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    The problem with that approach is a matter of consistency.

    If I have an object variable, and I want to set 5 different properties, I want to set them all with the same code.

    In my opinion:
    [php]<?php
    //Best:
    $Object->setA($A);
    $Object->setB($B);
    $Object->setC($C);
    $Object->setD($D);
    $Object->setE($E);
    Or go fluent...

    PHP Code:
    <?php
    $Object
    ->setA($A)
        ->
    setB($B)
        ->
    setC($C)
        ->
    setD($D)
        ->
    setE($E);
    Though I only consider adding getters and setters (or public properties) as a last resort.

  8. #33
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Maybe, but I don't really like chaining too much.

    In fact, my setter functions sometimes return the final property value, e.g:
    PHP Code:
    public function setNumber($Number){
        
    $this->Number = (int)$Number;
        return 
    $this->Number;

    Or, other times, if it is successful, e.g
    PHP Code:
    public function setPermissions($Permissions){
        if(!
    Authorised()){
            return 
    false;
        }else{
            
    //set permissions
            
    return true;
        }

    Rough example, true.

    But my point there is that if you rely on chaining, you may have to implement extra methods and properties stating whether an operation is successful - Throwing an Exception is one way, though isn't always appropriate.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  9. #34
    SitePoint Member
    Join Date
    Sep 2009
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is no inconsistency if you think about calling setters as method invocations (which they are), while thinking about using public properties as variable use (which it is).

    Besides, using getters/setters leads to significant increase in amount of code. If you're not doing anything extra in those methods, it's an increase for nothing.

    Code PHP:
    //clear
    $object->strings[] = "another string";
     
    //obfuscated
    $strings = $object->getStrings();
    $strings[] = "another string";
    $object->setStrings($strings);

    It looks okayish in small examples, but when I'm facing with large amount of code I need to understand, simple assignments make it much easier. They are a familiar syntax with well-defined meaning, rather than yet another method that can do whatever.

  10. #35
    SitePoint Addict SirAdrian's Avatar
    Join Date
    Jul 2005
    Location
    Kelowna, BC
    Posts
    289
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In that case I'd use something like
    PHP Code:
    $object->addString($string); 
    Adrian Schneider - Web Developer

  11. #36
    SitePoint Evangelist
    Join Date
    Mar 2006
    Location
    Sweden
    Posts
    451
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Gambler Z View Post
    I only use getX and setX when they have logic besides getting and setting variables. Otherwise I make properties public. It ain't Java, it's PHP. The result is 1) faster 2) more readable 3) more expressive.

    Code:
    $money = $wallet->getMoney(); //does this gets money out, or just gets them?
    $money = $wallet->money; //now I know for sure
    
    $wallet->setMoney(functionX($a, functionT($x), $y)); //need to count braces
    $wallet->money = functionX($a, functionT($x), $y); //don't need to count braces
    While I agree that dumb setters and getters are code bloat, it can be difficult to tackle a new requirement with public properties. What you solve with:
    $obj->money = $money;

    may work today, but someday you might need to validate what the money property is set to. That is, you might need a setter. Then you need to change all:
    $obj->money = $money;
    to:
    $obj->setMoney($money);
    or do some __set() magic.


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
  •