This strategy is much more reliable
than leaving it to the end user to provide valid information.
Protected
Just like functions often require variables intended for use only within the function,
classes can include fields used for solely internal purposes. Such fields are deemed
protected and are prefaced accordingly. An example follows:
172 CHAPTER 6 ?– O BJECT-ORIENTED PHP
class Employee
{
protected $wage;
}
Protected fields are also made available to inherited classes for access and manipulation,
a trait not shared by private fields. Any attempt by an object to access a protected
field will result in a fatal error. Therefore, if you plan on extending the class, you
should use protected fields in lieu of private fields.
Final
Marking a field as final prevents it from being overridden by a subclass, a matter
discussed in further detail in the next chapter. A finalized field is declared like so:
class Employee
{
final $ssn;
}
You can also declare methods as final; the procedure for doing so is described in
the later section ???Methods.???
Properties
Properties are a particularly convincing example of the powerful features OOP has to
offer, ensuring protection of fields by forcing access and manipulation to take place
through methods, yet allowing the data to be accessed as if it were a public field.
These methods, known as accessors and mutators, or more informally as getters and
setters, are automatically triggered whenever the field is accessed or manipulated,
respectively.
Pages:
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253