Patterns For PHP
Index Patterns News Forums

Talk:Transfer Object

From Patterns For PHP

php
public function testNoNewPublicProperties ()
    {
        $row = new User_Row;
        try {
 
            $row->users_name = 1;
 
        } catch (Exception $e) {
 
            $this->assertType('UnexpectedValueException', $e);
            return;
        }
        $this->fail('No exception thrown');
    }
 
    public function testDefaultValues()
    {
        $row = new User_Row;
        $this->assertNull($row->user_name);
    }
 
    public function testUnsettingOnlyRemovesValues()
    {
        $row = new User_Row('paul');
        $this->assertEquals($row->user_name, 'paul');
        unset($row->user_name);
        $this->assertNull($row->user_name);
        $row->user_name = 'paula'; // this wouldn't work right now
        $this->assertEquals($row->user_name, 'paula');
    }
    
    public function testUnsettingMakesItDirty()
    {
        $row = new User_Row('paul');
        unset($row->user_name);
        $this->assertTrue($row->isDirty());
    }

Here a class that behaves like this:

php
class User_Row
{
 
    private $data = array(
        'user_name'     => null,
        'user_password' => null,
        'user_email'    => null,
        'user_url'      => null
    );
 
    private $isDirty = false;
 
    public function __construct($name=null, $password=null, $email=null, $url=null)
    {
        $this->data['user_name']     = $name;
        $this->data['user_password'] = $password;
        $this->data['user_email']    = $email;
        $this->data['user_url']      = $url;
    }
 
    private function __get($name)
    {
        if (!array_key_exists($name, $this->data))
            throw new UnexpectedValueException($name);
 
        return $this->data[$name];
    }
 
    private function __set($name, $value)
    {
        if (!array_key_exists($name, $this->data))
            throw new UnexpectedValueException($name);
 
        if ($this->data[$name] !== $value) {
 
            $this->data[$name] = $value;
            $this->isDirty = true;
        }
    }
 
    private function __isset($name)
    {
        return isset($this->data[$name]);
    }
 
    private function __unset($name)
    {
        if (array_key_exists($name, $this->data)) {
 
            $this->data[$name] = null;
            $this->isDirty = true;
        }
    }
 
    public function isDirty() {
        return $this->isDirty;
    } 
}

And one question: Why are the magic methods declared as private? They are also declared as public in the examples of the english manual page but:

In PHP 5.0.x, all overloading methods must be defined as public.

--Basti 16:44, 28 December 2006 (PST)

Hi,

I edited the magic methods to be public, but it shouldn't be more than a 5.0.x fix. Since 5.1 AFAIK, they are allowed to be non-public - unless there is a valid reason to allow users directly call __get()/__set().

Since I prefer a standard, public is commonly used in the Zend Framework and other working code - so I've made the switch for now.

At some point I'll review this and other articles to add clarity - very good points with the revised unit tests ;).

--Pádraic 06:43, 24 January 2007 (PST)

MediaWiki
GNU Free Documentation License 1.2