Professional PHP

PHP Programming, Web Development, PHP Advocacy and PHP Best Practices.
« Where do you get your Wi-Fi?
php|tek Slides »

Let Your Properties be Properties

May 8th, 2007

There is a coding pattern that I see (and have used) in PHP code that defines generic methods on a class for setting and getting properties.

 
function set($name, $value);
function get($name);
 
Google code search for examples

Some times there are some ancillary methods to deal with unsetting, checking for existence, setting via an array, or dealing with references in PHP 4. They can really clutter up the definition of a class. That’s not good. All this code is fairly standard, too, but it gets duplicated on every class that does this. That’s not good, either.

Oh, I’ll solve this problem by making a base class, some may say. Wrong. This a very feeble reason to spend your one shot at inheritance. Trust me, I know, I’ve done it.

I think the idea is to make the class extensible. But PHP is really ok with just setting new properties on a class.

 
$obj->foo = 'bar';
 

So why not just do this?

Another variation of this pattern is to use setXXX($name, $value) or setYYY($name, $value) methods. This happens alot with “options” or “vars” or “properties.” It also happens on request wrapper classes. To me this looks like there is an object here just begging to get out for each XXX and YYY.

 
$obj->xxx->prop = 'foo';
$obj->yyy->prop = 'foo';
$obj->zzz->prop = 'foo';
 

This eliminates a slew of property manipulation methods and leaves the original class free to implement its true purpose. Methods of the form getXXX($name) and setXXX($name, $value) should be the solution of last resort.

Since I’ve started eliminating these in my own code in favor of direct properties, intermediate objects or __set and __get, I feel I’ve seen nothing but positive results. Try it. You may like it, too. Let your properties be properties.

UPDATE:
From reading the comments, I think there was some confusion about what I meant in this post. I am not talking about using naked properties instead of accessor methods. I’m not talking about accessor methods at all in this post. A specific accessor method, such as

$obj->getFoo()

where the name of the property is part of the method name is very different than
$obj->get('foo');

Where you pass the name of the property as a string parameter. Its only the latter pattern, where the property name is an actual parameter to the method, that I am talking about in this post and that I think should generally be refactored.

Filed Under

  • PHP, Software Design

Related Posts

  • Status of WACT
  • OOP is Mature, not Dead
  • Delphi for PHP
  • The PHP scalability saga continues
You can leave a response, or trackback from your own site.

15 Responses to “Let Your Properties be Properties”

  1. Matthew Turland says:
    5/8/2007 at 11:21 am

    IIRC, the original reasoning behind the use of get and set methods was to encapsulate the logic involved in obtaining or storing data in case that logic ever had to be changed, more commonly such that it was more complex. Granted, __get and __set are available in PHP 5, but those can build up clutter over time as classes grow. Seems better to just head it off at the pass by having the methods to start with. Granted, it makes the classes a little larger, but I think I’d rather have that than have to refactor classes further down the road because they became too cumbersome to maintain. Even in cases where all these methods do is get or set a class property to start with, it’s better to be consistent. Such cases, even though it seems unlikely they will ever change, can always come back and surprise you.

  2. Matthew Weier O'Phinney says:
    5/8/2007 at 1:27 pm

    I prefer using direct property access myself. However, in most of my real-world scenarios, I find myself writing accessors ((set|get)*()) as sooner or later I end up extending the class and adding business logic surrounding the property.

    For instance, I may need to validate that a value to which I am setting a property follows particular rules — if I allow direct access to the property, another programmer could bypass these checks, leading to errors and potential data corruption.

    Also, using an accessor to retrieve a property means I can later extend the accessor to do some logic prior to returning the property. As an example, a call to getResponse() could be modified to instantiate a response object if none is already defined in the object — much more useful than getting a null value. Or I might want to check that valid view paths are registered before I return a view — throwing an exception if not.

    Basically, accessors have their place, particular when working with classes meant for extending or consumption by third parties.

  3. Noel Darlow says:
    5/8/2007 at 7:46 pm

    I’m strongly in favour of getFoo() / setFoo() because I think it’s important to have a well-defined interface. Named getters/setters might explain the intent of the object a bit better, may accumulate additional logic as mentioned above, and can be mocked in tests. It’s surely one of the fundamentals of OOP: you can’t do anything with an object except via an interface?

  4. Jeff says:
    5/8/2007 at 8:32 pm

    Just to be clear, I’m not against accessor methods. However, a specific accessor method, such as

    $obj->getFoo()

    where the name of the property is part of the method name is very different than
    $obj->get('foo');

    Where you pass the name of the property as a string parameter. Its only the latter pattern that I am talking about in this post and that I think should generally be avoided.

  5. Joppe says:
    5/9/2007 at 12:30 am

    I don’t understand what you’re doing.
    If you make a class that’s extensible then you’re making a base class, or am i wrong? Do you mean that you use the possibility of PHP to set new (not existing) properties of a class?
    What is setXXX? Are you setting a property xxx of a class, is xxx an existing property or is it a new property, is xxx an object?
    How do you use it, $obj->setXXX( ‘prop’ , ‘foo’ ) ; ?

  6. Sebs says:
    5/9/2007 at 2:11 am

    the get/set pattern is that what i hate.
    its slow (function calls are slow)
    its obfuscating the code
    its so javaesk
    its ignoring the fact that there are the __get etc. methods

    so another one saying NO to the get/set schema

  7. Chris says:
    5/9/2007 at 3:53 am

    i am undecided if magic methods are a good thing. i am using them but they get quite messy if inheritance comes into play. mostly i end up writing protected setters to encapsulate them better and make sure that inheriting classes have a well defined interface to override a setter.

    how do you handle inheritance with your magic methods?

  8. PHPDeveloper.org says:
    5/10/2007 at 5:13 am

    Jeff Moore’s Blog: Let Your Properties be Properties

  9. developercast.com » Jeff Moore’s Blog: Let Your Properties be Properties says:
    5/10/2007 at 5:54 am

    [...] a recent post to his blog, Jeff Moore advocates the philosophy that, in your OOP application development, you [...]

  10. David says:
    5/24/2007 at 10:39 am

    I understand the logic to this, but it’s going to be more tedious as you get into larger applications with many classes vars and (of course) properties… For every var to be treated individually like that may be daunting.

    If you’re writing an application that’s just going to sit and function (little to no maintenance), then I don’t know how worthwhile it’d be to use this method. Though for larger applications that undergo maintenance and upgrading/updating, I think that this is very acceptable, and might help avoid problems with making edits down the road.

  11. Wolkje.net Weblog » Blog Archive » Accessing properties in PHP objects says:
    10/29/2007 at 2:20 am

    [...] I stumbled upon a weblog post of Jeff Moore on the way properties of objects should be accessed in PHP. Accidently, I thought a little about this problem myself last week because I’m working on a [...]

  12. Sebas says:
    10/30/2007 at 4:21 am

    I’m strongly in favour of getFoo() / setFoo() because I think it’s important to have a well-defined interface.

    How would you program this to an interface.
    Getters and setters are conventions, not interface implementations, or am I missing your point?

  13. pablo says:
    3/16/2008 at 9:36 am

    Hi, you can have all the getXXX/setXXX methods without declaring them, just using the magic function __call and cutting the “get” or “set” part from the name of the method invoked and doing the $obj->XXX = value internally in __call. That’s what I do in my projects.

    cheers,
    Pablo.

  14. MMF says:
    11/23/2008 at 12:46 pm

    Nice

  15. jessica says:
    8/3/2010 at 12:03 pm

    If you’re writing an application that’s just going to sit and function (little to no maintenance)

Leave a Reply

Click here to cancel reply.

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

code: use [code=php][/code].

Comment Preview

    Subscribe Feed
    Share Subscribe to this blog…
    Share Bookmark or share this page…
  • About

    My name is Jeff Moore. I'm a PHP programmer living in San Francico and working for a startup.

    More about me…

  • Categories (Home)

    • Agile Methods (14)
    • Mac (14)
    • Misc (17)
    • Open Source (14)
    • PHP (98)
    • Software Design (29)
    • Usability (14)
    • Web Design (20)
  • Recent Comments

    • Programming Language Trends via Google  19
      Craigslist pva, jessica, Scott [...]
    • Looking Towards the Cloud  35
      bentonville multiple listing, cosmetic dental, Sam Brodish [...]
    • PHP versus ASP  8
      Marhta Blight, Ravi, Ryan Brooks [...]
    • How to Transfer Mac OS X Application Data between Computers  59
      Website Migration, harry the computer support guy, Dotty Salvage [...]
    • Working with PHP 5 in Mac OS X 10.5 (Leopard)  157
      lehuuphuc, Robert Parthemer, Lingerie Intimate [...]
    • PHP Games  25
      jessica, Tennille Cranor at Chilli Plants, Lucas Ortell [...]
    • un-PEAR-ing  5
      jessica, Eugene Panin, Arnaud [...]
    • The Legality of Republishing RSS Feeds  23
      kevinxiao, Marissa Miscovich, Quick Student Loans [...]
    • Faster Page Loading  4
      jessica, angular cheilitis, Aaron Rosenfeld [...]
    • PDO versus MDB2  15
      jessica, kevinxiao, Gavin [...]
  • Recent Posts

    • ZendCon: Writing Maintainable PHP Code
    • Looking Towards the Cloud
    • Holiday Tech Support
    • Closures are coming to PHP
    • php | tek Wrapup
    • php | tek 2008
    • Sarah Snow Stever
    • Benchmarking PHP’s Magic Methods
    • The Endpoints of the Scale of Stupidity on Video
    • Working with PHP 5 in Mac OS X 10.5 (Leopard)
  • Site

    • Archives
    • Log in
  • Search