-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathch9p3Constants, Statics & Props.php
More file actions
160 lines (153 loc) · 3.46 KB
/
ch9p3Constants, Statics & Props.php
File metadata and controls
160 lines (153 loc) · 3.46 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
include_once('generalIncludes.php');
echo '<input id="chapter" type="hidden" value="9">';
echo '<h2>Chapter 9 OOP Programming in PHP - Paragraph paragraph Constants, Statics & Propsp</h2>';
echo '<h3>Listing 9.9: Static properties - The wrong way</h3>';
showcode(<<<'CODE'
class foo
{
static $bar = "bat";
public static function baz() {
echo "Hello World\n";
}
}
$foo = new foo();
$foo->baz();
echo $foo->bar;
CODE
);
echo '<h3>Listing 9.9: Static properties - The correct way with the paanayim nekudotayim ::</h3>';
showcode(<<<'CODE'
class foo2
{
static $bar = "bat";
public static function baz() {
echo "Hello World";
}
}
$foo = new foo2();
$foo::baz();
echo $foo::$bar;
CODE
);
echo '<h3>Listing 9.10: Static binding</h3>';
showcode(<<<'CODE'
class a
{
public static function test() {
self::foo();
self::bar();
}
public static function foo() {
echo __METHOD__ . " called by foo->a\n";
}
private static function bar() {
echo __METHOD__ . " called by bar->a\n";
}
}
class b extends a { }
class c extends a
{
public static function foo() {
echo __METHOD__ . " called by foo->c\n";
}
private static function bar() {
echo __METHOD__ . " called by bar->c\n";
}
}
a::test(); // a::foo called \n // a::bar called
b::test(); // a::foo called \n // a::bar called
c::test(); // a::foo called \n // a::bar called
CODE
);
echo '<h3>Listing 9.11: Late static binding</h3>';
showcode(<<<'CODE'
class x
{
public static function test() {
static::foo();
static::bar();
}
public static function foo() {
echo __METHOD__ . " called by foo->x\n";
}
private static function bar() {
echo __METHOD__ . " called by bar->x\n";
}
}
class y extends x { }
class z extends x
{
public static function foo() {
echo __METHOD__ . " called by foo->z\n";
}
private static function bar() {
echo __METHOD__ . " called by bar->z\n";
}
}
x::test(); // x::foo called \n // x::bar called
y::test(); // x::foo called \n // x::bar called
z::test(); // z::foo called \n // Fatal error: Call to private method
// z::bar() from context 'x'
CODE
);
echo '<h3>Listing 9.14: Using reflection</h3>';
showcode(<<<'CODE'
/**
* Say Hello
*
* @param string $to
*/
function hello($to = "World") {
echo "Hello $to";
}
$funcs = get_defined_functions();
?><h1>Documentation</h1>
<?php
/**
* Do Foo
*
* @param string $bar Some Bar
* @param array $baz An Array of Baz
*/
function foo($bar, $baz = array()) { }
$funcs = get_defined_functions();
foreach ($funcs['user'] as $func) {
try {
$func = new ReflectionFunction($func);
} catch (ReflectionException $e) {
// ...
}
$prototype = $func->name . ' ( ';
$args = array();
foreach ($func->getParameters() as $param) {
$arg = "";
if ($param->isPassedByReference()) {
$arg = '&';
}
if ($param->isOptional()) {
$arg = '[' . $param->getName()
. ' = '
. $param->getDefaultValue() . ']';
} else {
$arg = $param->getName();
}
$args[] = $arg;
}
$prototype .= implode(", ", $args) . ' )';
echo "<h2>$prototype</h2>
<p>
Comment:
</p>
<pre>" . $func->getDocComment() . "</pre>
<p>
File: " . $func->getFileName() . "<br />
Lines: " . $func->getStartLine() . " - " . $func->getEndLine() . "
</p>";
}
CODE
);
echo '<h3></h3>';
showcode(<<<'CODE'
CODE
);