All in the <head>

– Ponderings & code by Drew McLellan –

– Live from The Internets since 2003 –

About

PHP class properties

26 October 2003

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.

- Drew McLellan

Tags

Comments

  1. § Kyle Adams: Drew—I’d recommend reading Kyle Vanderbeek’s dissertation on why getters and setters are A Good Thing; informative and funny to boot.

    http://www.calvin.edu/archive/abstraction-chat/200302/0216.html
  2. § Drew: Thanks, Kyle. That’s a good read.
  3. § Dan Benjamin: Drew,

    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 ;-)
  4. § George: The full point is Encapsulation you hide the inner workings of a class from the user, and you only allow them to access certain aspects through the interface you create.

    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
  5. § Drew: George, I hear you, and yes I appreciate the need for encapsulation.

    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.
  6. § Jordan: The limitation on defaults for function arguments isn’t limited to object properties—as per the documentation, ”[an argument’s] default value must be a constant expression, not (for example) a variable, a class member or a function call.”

    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.
  7. § David Weingart: I hate that PHP has no way to declare variables and will not error if you use a variable without declaring it first.

    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).
  8. § Drew: Thanks David. However, I can’t see how to declare variables from the documentation. How do you do it?
  9. § zlog: $myvar = NULL;

    $myvar = ”select id, username from accounts where date
  10. § Drew: Thanks Ronan. No exactly a thing of beauty, is it?
  11. § george: It’s not the most graceful thing in coding. But it’s good practice. I would prefer PHP wen down the Java (stongly typed) route and insisted you declared your variable types.

    G

Photographs

Work With Me

edgeofmyseat.com logo

At edgeofmyseat.com we build custom content management systems, ecommerce solutions and develop web apps.

Recent Links

Affiliation

  • Web Standards Project
  • Britpack
  • 24 ways

About Drew McLellan

Photo of Drew McLellan

Drew McLellan has been hacking on the web since around 1996 following an unfortunate incident with a margarine tub. Since then he’s spread himself between both front- and back-end development projects, and now is Director and Senior Web Developer at edgeofmyseat.com in Maidenhead, UK (GEO: 51.5217, -0.7177). Prior to this, Drew was a Web Developer for Yahoo!, and before that primarily worked as a technical lead within design and branding agencies for clients such as Nissan, Goodyear Dunlop, Siemens/Bosch, Cadburys, ICI Dulux and Virgin.net. Somewhere along the way, Drew managed to get himself embroiled with Dreamweaver and was made an early Macromedia Evangelist for that product. This lead to book deals, public appearances, fame, glory, and his eventual downfall.

Picking himself up again, Drew is now a strong advocate for best practises, and stood as Group Lead for The Web Standards Project 2006-08. He has had articles published by A List Apart, Adobe, and O’Reilly Media’s XML.com, mostly due to mistaken identity. Drew is a proponent of the lower-case semantic web, and is currently expending energies in the direction of the microformats movement, with particular interests in making parsers an off-the-shelf commodity and developing simple UI conventions. He writes here at all in the head and, with a little help from his friends, at 24 ways.