PHP5 and Accessors

[ rakaur on Sat Sep 11 at 11:12 AM // category: programming, technology ]

Okay, this is an addition to my previous post, but I figure it’s so immediately retarded that it deserves its own post.

PHP5 “solves” the problems of accessors with __get() and __set(). Say you have this code in PHP:

class Person
{
    private $name;

    public function setName($name)
    {
        $this->name = $name;
    }

    public function getName()
    {
        return $this->name;
    }
}

Okay, so, putting aside that this is the most retarded thing on the planet, it’s pretty kludgy. What happens when you have a dozen properties? You have two dozen accessors. Well, that sucks more than the purpose of accessors themselves! I mean. if you’re feeding the stupid thing the value in the first place what the fuck is even the point of making the property private?!

But, PHP5 goes forth and makes this even more retarded:

class Person
{
    private $data;

    public function __set($property, $value)
    {
        $this->data[$property] = $value;
    }

    public function __get($property)
    {
        if (isset($this->data[$property]))
            return $this->data[$property];

        else
            return FALSE;
    }
}

So, this code sort of provides virtual accessors. That is, you feed it some undefined property (in this case, a nonexistant member of data) that you want to retrieve and/or modify, and it creates the property and does it for you.

But wait! You want to use inheritance?! Sorry, but, the only thing your child class is going to get is an empty data array! And __set() and __get() only work on nonexistant properties! Sorry, you’re screwed!

Gee, I know, I’ll just write my own accessors. I’ve only done that a thousand times.

But in either case, can anyone tell me the fucking point of this? How is this any different than making the varaible public to start with? How is any accessor any different than making the variable public? Why is this so fucking stupid? Am I missing some gaping logic and reason here or something?

-- rakaur // 2004.09.11 @ 11:12 AM


0 TrackBacks

Listed below are links to blogs that reference this entry.

TrackBack URL for this entry: http://mt.ericw.org/mt-tb.cgi/25


Leave a comment


Name:
Email:
URL:   
Remember personal info?