PHP has a pretty basic class model. You can define classes and create methods as functions within the class. You can also define properties (aka attributes), although in a fairly loose and seemingly uncontrolled way. Users can instantiate the class and then get and set the properties as they wish.
However, I just read that it’s bad form to let users read and write to properties directly, as this should always been done through a method. That is to say rather than saying $myClass->email=\'admin@example.com\'; you should more properly do something akin to $myClass->setEmail(\'admin@example.com\');.
I guess that’s a sensible idea as PHP provides no inherent mechanisms for marshaling values in and out of the properties without resorting to using methods. It does nevertheless seem a little cumbersome to have to create a get and set method for every property. Blurgh.
Something else that’s getting on my tits today is the inability to directly set a property as the default value for an method attribute. That is to say, you can’t do this:
function sendEmail($email = $this->email){
// foo;
}
Instead, you have to something ugly like this:
function sendEmail($email = \'\'){
if (!$email){
$email = $this->email;
}
// foo;
}
This is less than ideal, and although it hasn’t quite spoiled my afternoon, it did make me growl at the wall for a bit. Ho hum. I’m sure it’s all positive really.



Comments
http://www.calvin.edu/archive/abstraction-chat/200302/0216.html
That’s a great article, and, if you plan on moving from PHP into Java, you should get used to getter and setter methods - that’s really the OO way to do things.
Welcome to the tedium that is the life of an object oriented developer ;-)
I would say I spend 90% in Java 10% in PHP and it’s frustrating, moving back to PHP and trying to write a class, only to realise the restrictions. Hopefully PHP5 will address this.
George
I find the limitations of PHP frustrating, but only because I like PHP so much. It’s so agile. I don’t get too frustrated about ASP because basically I couldn’t care less about it. I hate that PHP has no way to declare variables and will not error if you use a variable without declaring it first. It’s downright dangerous. It makes me nervous with every line of code I write.
You’re right .. I hope PHP5 is better.
I’m surprised that nobody else has pointed this out, but in the upcoming version 5, PHP’s object model is becoming quite a lot more robust and, dare I say, Java-like, which includes ”private” and ”protected” keywords, as well as ”type hinting” to restrict what sort of data which can be passed as an argument to a function.
Concerning getters and setters, I can’t fathom needing to write a getter and setter for every class variable, but in a language as dynamically-typed as PHP, I find that getters and setters are very useful for ”validating” what goes in and out of an object.
Try this:
error_reporting(E_ALL)E_ALL can also be set in your PHP config file (PHP.ini, or on a per-site basis using Apache config files).
$myvar = ”select id, username from accounts where date
G