Here we
define two constant names ASC and DESC whose values are 1 and 2 respectively.
To access these constants from within the class, we reference them with the self
keyword. Please note that we are accessing them with the :: operator, not a ->
operator, because these constants act like a static member.
Finally to use this class, let's create a snippet as shown below. In this snippet we are
also accessing those constants:
include_once("class.wordcounter.php");
$wc = new WordCounter("words.txt");
$wc->count(WordCounter::DESC);
?>
Please note that we are accessing the class constants from outside the class by
following the :: operator right after the class name, not after the instance of the class.
Now let's test the script, please create a file named words.txt with the following
content in the same directory where you placed the above script:
words.txt
Wordpress is an open source blogging engine. If you are not familiar
with blogging, it is something like keeping a diary on the web.
A blog stands for web log. Wordpress is totally free and
released under the GPL.
Now, if you execute the usage script, this time, you will see the following output.
is = 3
a = 2
blogging = 2
web = 2
wordpress = 2
Kick-Starting OOP
[ 28 ]
stands = 1
blog = 1
in = 1
diary = 1
for = 1
free = 1
under = 1
gpl = 1
released = 1
and = 1
totally = 1
log = 1
something = 1
if = 1
you = 1
engine = 1
source = 1
an= 1
open = 1
are = 1
not = 1
?? = 1
like = 1
it = 1
with = 1
familiar = 1
keeping = 1
Nice utility, what do you think?
Extending a Class [Inheritance]
One of the greatest features in OOP is that you can extend a class and create a
completely new object.
Pages:
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