Low cost ecommerce web development India flash website design
To use the class, we've instantiated it with the new keyword. The object created
from the class is placed in the $webPage variable. Through this variable, we have
access to all the members of the object as we did with the $this variable above.
The first call to the addHeader method demonstrates the point:
$webPage->addHeader('A Page Built with an Object');
Only at the end, upon calling the get method, do we actually get anything back
from the class. No longer do we need to worry about passing around a variable
that contains the contents of the page—the class takes care of that.
Avoid output in classes
Instead of get, we could have endowed the Page class with a method called
write to send the page code to the browser immediately. This would have
made the code above slightly simpler, as the main script would not have had
to get the code from the object and echo it itself. We avoided this for a
reason.
It's usually a bad idea to output directly from inside a class (with statements
and functions such as echo and printf); doing so will reduce the flexibility
of your classes. Allowing the value to be retrieved from the class gives you
the option of performing additional transformations on it before you send
it to the browser, or use it for some other purpose entirely (like putting it in
an email!).
Notice also that the number of lines of code we have to write to use the class is
fewer than were required in the earlier examples. Although it's impossible to determine
good application design by counting the number of lines of code, it is
clear that the class has made the procedural code that uses it much simpler. From
the point of view of people reading the code, it's already fairly clear what's going
on, even without them having to look at the code for the Page class.
website designer freelance ASP PHP ecommerce web developer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110