That's it.
So you might ask why someone does these extra jobs, when they can easily set these
variables as public and leave everything else as is. Aren't all these the same? Well,
no. Using accessor methods, you get some extra benefits. You will have full control
while setting or retrieving the value of any property. "So what?" You might ask. Let's
use a scenario where you need to filter users' input and set into properties. In this
case, a setter can help you to filter the input before setting them into work.
Does this mean we have to write 100 getter and setter methods if my class
contains 100 properties? You ask as good question. PHP is kind enough to relieve
you from this boredom. How? Let us see the next section where we discuss using
magic methods for setting and getting property values dynamically. Those methods
will reduce the stress up to 90%. Don't you believe me? Let's see.
Kick-Starting OOP
[ 40 ]
Using Magic Methods to Set/Get Class
Properties
We discussed in the previous section that writing accessor method for a number
of properties will be a real nightmare. To avoid that boredom, you can use magic
methods. This process is called property overloading.
Pages:
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63