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);
break;
case "lname":
$this->lname = strtoupper ($value);
break;
.....
}

Thanks,

Mac

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:

<?php
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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.