Handle this typehints
[hiphop-php.git] / hphp / test / slow / stringish.php
blob36311b182cdf169fbdbc6a6b2b688e183079be45
1 <?hh
3 // This error handler swallows typehint errors, which is
4 // disallowed in RepoAuthoritative mode. Thus, this test
5 // is set to be norepo.
6 function err($code, $msg) {
7 echo "Handled ${code}: $msg", "\n";
8 return true;
10 set_error_handler('err');
12 class CExplicit implements Stringish {
13 public function __toString() {
14 return __CLASS__;
18 class CImplicit {
19 public function __toString() {
20 return __CLASS__;
24 trait TStringish {
25 public function __toString() { return __TRAIT__; }
28 interface IStringish {
29 public function __toString();
32 class CThruTrait {
33 use TStringish;
36 function f1(?Stringish $x): void {
37 $s = Stringish::class;
38 echo ($x instanceof Stringish) ? "true" : "false", ", ";
39 echo ($x instanceof $s) ? "true" : "false", ", ";
40 var_dump($x);
41 echo "\n";
44 function f2(Stringish $x): void {
45 $s = Stringish::class;
46 echo ($x instanceof Stringish) ? "true" : "false", ", ";
47 echo ($x instanceof $s) ? "true" : "false", ", ";
48 var_dump($x);
49 echo "\n";
52 function test_functionality() {
53 echo '********** static string **********', "\n";
54 f1("a boring string");
55 f2("a boring string");
57 echo '********** dynamic string **********', "\n";
58 $x = "hello ";
59 if (time() > 0) {
60 $x .= "world";
62 f1($x);
63 f2($x);
65 echo '********** explicit implements **********', "\n";
66 $explicit = new CExplicit();
67 f1($explicit);
68 f2($explicit);
70 echo '********** implicit implements **********', "\n";
71 $implicit = new CImplicit();
72 f1($implicit);
73 f2($implicit);
75 echo '********** via trait implements **********', "\n";
76 $via_trait = new CThruTrait();
77 f1($via_trait);
78 f2($via_trait);
80 echo '********** null **********', "\n";
81 f1(null);
82 f2(null);
84 echo '********** array **********', "\n";
85 f1(array(1, 2, 3));
86 f2(array(1, 2, 3));
88 echo '********** number **********', "\n";
89 f1(10);
90 f2(10);
91 f1(-4.2);
92 f2(-4.2);
95 function test_reflection() {
96 echo "\n",
97 '--------------------', ' ', __FUNCTION__, ' ', '--------------------',
98 "\n\n";
99 var_dump(interface_exists(Stringish::class));
101 var_dump(is_a(CExplicit::class, Stringish::class));
102 var_dump(is_subclass_of(CExplicit::class, Stringish::class));
104 var_dump(is_a(CImplicit::class, Stringish::class));
105 var_dump(is_subclass_of(CImplicit::class, Stringish::class));
107 $rc = new ReflectionClass(CExplicit::class);
108 var_dump($rc->getInterfaceNames());
109 var_dump($rc->implementsInterface(Stringish::class));
110 var_dump($rc->isSubclassOf(Stringish::class));
112 $rc = new ReflectionClass(CImplicit::class);
113 var_dump($rc->getInterfaceNames());
114 var_dump($rc->implementsInterface(Stringish::class));
115 var_dump($rc->isSubclassOf(Stringish::class));
117 $rc = new ReflectionClass(CThruTrait::class);
118 var_dump($rc->getInterfaceNames());
119 var_dump($rc->implementsInterface(Stringish::class));
120 var_dump($rc->isSubclassOf(Stringish::class));
122 $rc = new ReflectionClass(TStringish::class);
123 var_dump($rc->getInterfaceNames());
124 var_dump($rc->implementsInterface(Stringish::class));
125 var_dump($rc->isSubclassOf(Stringish::class));
127 $rc = new ReflectionClass(IStringish::class);
128 var_dump($rc->getInterfaceNames());
129 var_dump($rc->implementsInterface(Stringish::class));
130 var_dump($rc->isSubclassOf(Stringish::class));
134 test_functionality();
135 test_reflection();