self::$id."\n";
}
}
$st1 = new StaticTester();
StaticTester::checkIdFromStaticMehod();
Kick-Starting OOP
[ 38 ]
$st2 = new StaticTester();
$st1->checkIdFromNonStaticMethod(); //returns the val of $id as 2
$st1->checkIdFromStaticMehod();
$st2->checkIdFromNonStaticMethod();
$st3 = new StaticTester();
StaticTester::checkIdFromStaticMehod();
?>
You will see the output is as follows:
Current Id From Static Method is 1
Current Id From Non Static Method is 2
Current Id From Static Method is 2
Current Id From Non Static Method is 2
Current Id From Static Method is 3
Whenever we create a new instance, it affects all the instances as the variable is
declared as static. Using this special facility, a special design pattern "Singleton"
works perfectly in PHP.
Caution: Using Static Members
Static members make object oriented much like old procedural
programming; without creating instances, you can directly call any
function, like the old days. That's why we use static method with caution.
Excessive static methods make no use at all. Unless you have any specific
purpose, don't use static members.
Accessor Methods
Accessor methods are simply methods that are solely devoted to get and set the
value of any class properties.
Pages:
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