RarestBlog

PHP Getters and Setters

Thanks to Lonny Kapelushnik I’ve discovered that C#-style getters and setters might be coming to PHP in some future. There is already an implementation. He mentioned it as a response to my previous article.

So, this might be possible in near PHP future:

1
2
3
4
5
6
7
8
9
10
11
class TimePeriod {
    private $Seconds;
 
    // Accessor properties are implemented just like you would define an actual property
    public $Hours {
        get { return $this->Seconds / 3600; }
 
        // In the set accessor, the variable $value holds the incoming value to be "set"
        set { $this->Seconds = $value * 3600; }
    }
}

Read about the cool solution here.