Prev | Current Page 242 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"


Unfortunately, PHP does not offer the property functionality that you might be
used to if you??™re familiar with other OOP languages such as C++ and Java. Therefore,
you??™ll need to make do with using public methods to imitate such functionality. For
example, you might create getter and setter methods for the property name by declaring
two functions, getName() and setName(), respectively, and embedding the appropriate
syntax within each. An example of this strategy is presented at the conclusion of
this section.
CHAPTER 6 ?–  O BJECT-ORIENTED PHP 173
PHP version 5 and newer does offer some semblance of support for properties,
done by overloading the __set and __get methods. These methods are invoked if you
attempt to reference a member variable that does not exist within the class definition.
Properties can be used for a variety of purposes, such as to invoke an error message,
or even to extend the class by actually creating new variables on the fly. Both __get
and __set are introduced in this section.
Setting Properties
The mutator, or setter method, is responsible for both hiding property assignment
implementation and validating class data before assigning it to a class field. Its prototype
follows:
boolean __set([string property name],[mixed value_to_assign])
It takes as input a property name and a corresponding value, returning TRUE if the
method is successfully executed, and FALSE otherwise.


Pages:
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254