/
ProxyMethod_test.php
73 lines (64 loc) · 1.8 KB
/
ProxyMethod_test.php
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
<?php
error_reporting(E_ALL);
require __DIR__ . DIRECTORY_SEPARATOR . "ProxyMethod.php";
define("EOL", PHP_EOL);
define("EOL2", str_repeat(PHP_EOL, 2));
class Base {
private function test() {
echo "test inheritance" . EOL;
}
private static function staticTest() {
echo "test inheritance, static" . EOL;
}
}
class Hello extends Base{
private static $name;
private function __construct($name, $option = null)
{
self::$name = $name;
}
public static function getIns($name, $option = null)
{
return new self($name, $option);
}
private static function hello($prefix) {
$name = self::$name;
echo "Hello, $prefix $name!" . EOL;
}
private function hi($prefix) {
$name = self::$name;
echo "Hi, $prefix $name!" . EOL;
}
}
echo "Test1" . EOL;
$test = Hello::getIns("xiaohong");
$say1 = new ProxyMethod($test);
$say1->hi("Miss."); // Hi, Miss. xiaohong!
$say1::hello("Miss."); // Hello, Miss. xiaohong!
$say1->test(); // test inheritance
$say1::staticTest(); // test inheritance, static
$say1->export("hello");
$say1->export("hi");
hi($test, "Miss."); // Hi, Miss. xiaohong!
hello("Miss."); // Hello, Miss. xiaohong!
echo EOL2;
echo "Test2" . EOL;
$say2 = new ProxyMethod("Hello", "xiaoming");
$say2->hi("Mr."); // Hi, Mr. xiaoming!
$say2::hello("Mr."); // Hello, Mr. xiaoming!
$say2->test(); // test inheritance
$say2::staticTest(); // test inheritance, static
$say2->alias("hi", "nihao1");
$say2->nihao1("Mr."); // Hi, Mr. xiaoming!
$say2->rename("hi", "nihao2");
$say2->nihao2("Mr."); // Hi, Mr. xiaoming!
//$proxy2->hi("Mr."); // Exception
echo EOL2;
echo "Test3" . EOL;
$stack = new ProxyMethod("SplStack");
foreach(range(0, 9) as $i) {
$stack->push($i);
}
foreach(range(0, 9) as $i) {
echo $stack->pop() . " ";
}