' ' . $this->last_name);
if (empty($name)) {
$name = $this->username;
}
return $name;
}
We can use name() directly, for example:
$rob = $users->find(1)->current();
echo $rob->name();
It would however be ???nicer??? to be able to treat the name as a read-only property of the record so that it is
used in exactly the same way as, say town. This will increase the predictability of the class as the programmer
will not have to think about whether a given property is a field in the database or a function within the class.
The easiest and most efficient way to do this is to override __get() as shown in Listing 5.8.
Listing 5.8: Overriding __get() to provide custom properties
class User extends Zend_Db_Table_Row_Abstract
{
//...
function __get($key)
{
if(method_exists($this, $key)) #1
{
return $this->$key(); #2
}
return parent::__get($key); #2
}
//...
}
(annotation) <#1. Check that there??™s a class method names the same as $key.>
(annotation) <#2. If so, call the function and return.>
Licensed to Menshu You
Please post comments or corrections to the Author Online forum at
http://www.manning-sandbox.com/forum.jspa?forumID=329
(annotation) <#3. If not, call through to the original implementation of __get().>
We have to call our parent??™s __get() function last as it will throw an exception if $key does not exist in the
database as a field.
Pages:
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167