C# style Auto-Properties in PHP

Hi gang!

So C# is really great. We all know this. PHP is really great too. But there are some annoying things about it. Like some other older languages (Java, I'm looking in your direction...) you have to create class Properties by hand. You have to create two methods: a getter and a setter, like so:

PHP
  1. public function getString()
  2. {
  3.     return $this->_string;
  4. }
  5.  
  6. public function setString($string)
  7. {
  8.     $this->_string = $string;
  9. }


Lammmmmmeeeeeeee! Imagine a container class that has 15 properties. You're hands would fall off, and you'd get fired. Try explaining to your boss that it took you 4.5 hours to code your properties. Yeah right sucka!

The C# auto-property does it right and would look like so:

C#
  1. public string String { get; set; }


Holy shit that's awesome! If you need to, you can put code inside both the get and set elements, and do whatever you need to to the data like this:

C#
  1. public string String
  2. {
  3.     get
  4.     {
  5.         return string;
  6.     }
  7.     set
  8.     {
  9.         // Do something interesting here
  10.         string = value;
  11.     }
  12. }

(notice the value keyword - it's special to properties.)

You can do that with PHP? Well, not quite, but you can get close, and if you use Netbeans, I can make it almost as fast to implement too. Just watch...

PHP
  1. public function Property($property = null)
  2. {
  3.     if($property != null)
  4.     {
  5.         $this->_property = $property;
  6.     }
  7.     return $this->_property;
  8. }

So what's going on here? Well, our auto-property is a regular ol' method. The method is overloaded by the default assignment of the argument, $property. If we don't pass in an argument (in the case of using our property to get the value), then the argument is automatically assigned null. with an if conditional we then check to see if the argument is null, and if it is, control will fall through the if block and simple return the value.

If the argument is not null, that means we're trying to assign a value to our property.

Wowie! What's cool about doing it this way is that it always returns the current value. Of course if you don't want it to return a value when you set the member variable, you can change it, but I like always getting the current value back with each call.

So that's it... You can get or set by calling one method, you can make it read-only, you can do any kind of data manipulation, type checking, or whatever you want before each get/set.

Now earlier on I told you can I make implementing this as fast (almost) as C#. Well, only if you have Netbeans, but yes. Netbeans has a neat-o code completion engine, and will allow you to type a keyword of your choosing, hit tab, and it will spit out some templates code for you to fill in. You can use it to create conditional blocks, new classes, new methods, all kinds of stuff, and we'll add a template to make properties for us:

In Netbeans, go to Tools -> Options -> Editor -> Code Templates. Select PHP from the Language drop down. Now, click new. Then, create the keyword (I use the keyword prop). In the Expanded Text tab, pase in the below code:
PHP
  1. public function ${Property}(${$var} = null)
  2. {
  3.     if(${$var} != null)
  4.     {
  5.         $this->${var} = ${$var};
  6.     }
  7.     return $this->${var};
  8. }


When you create one of these, Netbeans will walk you through setting the method name, the method argument, and the member variable name. It takes about 4 seconds to create one of these using the template :) Now that's progress. With the time I saved using this auto property/Netbeans combo on my project at work, I wrote this post, and took an extra smoke break. What can Netbeans and auto properties do for you?



Comments


The problem is when You want to set some property to null :D Nice post - almost same as my solution. The fix for that can be: public function Property($property = _customnull) { if($property != _customnull) {.... _customnull some constant with some very unique value ... Thanks, Greets