I'm new to PHP and trying to learn a bit about classes.

In most languages if a class member is private, it is accessed through a property which itself has an accessor. So there is a separate accessor for each property

It appears in PHP that the __set accessor is the only accessor available for an entire class.

Does this mean then that processing for every class member has to take place in this one accessor? In other words, it I want every member of the class to be stored in upper case does __set look something like this?

function __set($name,$value)
switch ($name)
case "fname":
$this->fname = strtoupper ($value);
case "lname":
$this->lname = strtoupper ($value);



7 Years
Discussion Span
Last Post by sergb

When writing classes you usually provide your own accessors and mutators (getters and setters) for each property that needs them. Methods that start with two underscores usually have special meaning and you should not override them unless you know exactly what you are doing. One exception to that is the __construct function, which is the constructor for your objects.

This is the usual way to do what you want:

class Person
    private $fname;
    private $lname;

    public function getFName() {
        return $this->fname;

    public function setFName($fname) {
        $this->fname = strtoupper($fname);

    public function getLName() {
        return $this->lname;

    public function setLName($lname) {
        $this->lname = strtoupper($lname);

This is not only true for PHP, but most other languages.
Hope this helps.

Edited by sergb: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.