.NET Style Properties (Getters/Setters) in PHP


DSC01202 On any given day, I can be programming in one of many languages (C# .NET, PHP, JavaScript, CSS, & HTML [I know I may be pushing it calling CSS & HTML languages], and soon Visual Basic .NET because of a collaborative project).  Don’t get me wrong, I love that part of my job.  It keeps me sharp.  Sure there is the occasional mishap where I forget that the . is the member accessor in the .NET languages, but it is the concatenation operator in PHP and I will throw the occasional $ into my C# code…

Another thing to know is that I am an object oriented programmer to the core…I am currently cleaning up after another developer who was not an object oriented guy…grr…there is data access code everywhere and he didn’t know the definition of object…finally I just took the functionality down because it was so bug ridden that whenever I would fix one bug, it would expose another.  One thing that has constantly bugged me since I started coding in PHP that I didn’t like its method of using __get and __set for getters and setters (mainly because I still use a modified version of Hungarian notation, but that is a discussion for another blog post, so I didn’t like what the getters and setters would look like).

Then, tonight, as I was coding, I had an epiphany, I could use PHP’s optional operator syntax for a function to get something close to what I would want.  So, where I would have coded this before:

   1:  <?php
   2:   
   3:  class Test {
   4:   
   5:  private $m_intId;
   6:   
   7:  public function setId($value) {
   8:   
   9:  $this->m_intId = $value;
  10:   
  11:  }
  12:   
  13:  public function getId() {
  14:   
  15:  return $this->m_intId();
  16:   
  17:  }
  18:   
  19:  }
  20:   
  21:  ?>

where I would do the following to access it $objTest->setId(1); or $intId = $objTest->getId(); (I would have to explicitly call the getter or setter)

Tonight I realized, I could code it this way:

   1:  <?php
   2:   
   3:  class Test {
   4:   
   5:  private $m_intId;
   6:   
   7:  public function Id($value = 0) {
   8:   
   9:  if($value != 0) {
  10:   
  11:  $this->m_intId = $value;
  12:   
  13:  } else {
  14:   
  15:  return $this->m_intId;
  16:   
  17:  }
  18:   
  19:  }
  20:   
  21:  ?>

Now, I can access the id by doing the following: $objTest->Id(5); or $intId = $objTest->Id();

What are your thoughts developers?

Share

  • daniel

    Well, I’m not really a coder any more but at the risk of sounding dumb and really only because I think it would be fun to get into the fray….

    Two thoughts: 1) I like stuff that does one thing only & does it very well. The Get/Set to me is the ultimate of this…no logic, just a straight do it….and only it. 2) I want to set the value to zero…two lines get evaluated to do this…not one…true? 3) At least in .Net VS2005 code coverage metrics around automated test scripts tend to suck when it comes to If/Then/Else constructs. Does this matter? Probably not, but again it points out that it’s potentially a less explicit way to do the work.

    OK, did I just magnify my ignorance or did any of this actually make sense?

  • Jeff Miles

    Dan,

    It totally makes sense, I do think though that there is some CLR runtime impact while the CLR determines whether it is a get or a set function but maybe not.

    I think what your question boils down to is this: Do you sacrifice developer speed & comfort for execution speed?

    I don’t know the performance impact of what I am proposing above, but you do have a good point. I will have to look into the performance impacts of what I am doing. Does me saving time coding justify the performance impact…interesting…

  • Gal

    i like the use of getters/setters, and i would rather having them as separate functions. One reason would be that in the setter i would sometime check for validity, ie is_numeric(), and return true/false.

    One thing to take in mind, when it comes to interpreted languages (this doesn’t apply to compiled languages), i would advise not using getters/setters for these kind of things, since it is much slower. I know this damages the object oriented approach, and i’m also an oop freak, but when you write large systems performance should be considered in every line of code.
    Calling a function would take about twice longer, since active context/scopes have to change etc.
    If the methods are not used in a loop or something then the it shouldn’t bother you too much though. other than that, both techniques are fine.

  • Jeff Miles

    Gal,

    Thanks for the input, this is good information to chew on. Like you said, I am an OOP freak as well so this is a huge deal (even though it probably shouldn’t be) to me…Thanks for the comment!

    Jeff