I'm a web developer, freelancer, author, speaker, entrepreneur, technical reviewer and blogger based in the North East, living in a village just outside of Chester-le-Street.

My blog

Generating Getters and Setters with PHP

In Eclipse, there is (or at least way) a feature to automatically generate getter and setter methods within a class. I use PHPEclipse, and that feature isn't available, and it really annoyed me. After some searching I stumbled upon an online tool, where you paste your variables in, and it generates the code, however, each getter/setter was on a single line, with no comments pre-populated above them, so I've written my own.

It is an online tool, simply paste in your variables and it gives you some nice code in return, I've used the JavaScript Syntax Higlighter to make it look pretty, and make it easy to copy the code.

The tool

PHP Getter and Setter Generator

I'll probably not be releasing the code for this right away, as it is a little messy ;)

Donate

Like this tool, want to give me some pennies?

Posted by Michael on 5th May 2010 at 11:11

Comments

You're going to hate me as I'm going pessimistic again... But I've never understood getters and setters. What purpose do they serve over a class property that's declared public?

Posted by Martin Bean on 5th May 2010 at 18:06

Looks good Michael. In a nutshell Martin, an object should be able to control its own state, by allowing direct, external, access to member properties, it cannot do this. Moreover, you're exposing the internals of the object to the outside world - a *big* no-no in OOP. More often than not, an object must hide its internal implementation to meet encapsulation requirements. http://en.wikipedia.org/wiki/Encapsulation_%28computer_science%29#Encapsulation

Posted by Anthony Sterling on 19th May 2010 at 11:11

Add a comment