PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/hphp/test/spec/tests/constants/constants.php

http://github.com/facebook/hiphop-php
PHP | 173 lines | 80 code | 33 blank | 60 comment | 3 complexity | c6e980ca2540d3637b8069cf8bc21f91 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, MIT, LGPL-2.0, Apache-2.0
  1. <?php
  2. /*
  3. +-------------------------------------------------------------+
  4. | Copyright (c) 2015 Facebook, Inc. (http://www.facebook.com) |
  5. +-------------------------------------------------------------+
  6. */
  7. error_reporting(-1);
  8. function trace($name, $value, $b = FALSE)
  9. {
  10. $r = define($name, $value, $b);
  11. echo "define $name " . ($r ? "succeeded" : "failed");
  12. if (defined($name))
  13. echo "; value is >" . constant($name) . "<\n";
  14. else
  15. echo "; not defined\n";
  16. }
  17. ///*
  18. // define some constants with simple (single-token) scalar initial values
  19. trace("STATUS1", TRUE);
  20. trace("MIN", 10);
  21. trace("MAX", 20, TRUE); // HHVM Warning: Case insensitive constant names are not supported in HipHop
  22. trace("MY_PI", 3.1415926);
  23. trace("MY_COLOR", "red");
  24. trace("C1", NULL);
  25. //*/
  26. ///*
  27. // try to define some constants with multiple-token scalar initial values
  28. // involving literals only
  29. trace("CON1", 5 + 10.1 * 3);// succeeded;
  30. // involving variables and other non-basic operators
  31. function getValue() { return 250; }
  32. $v1 = 10;
  33. trace("CON2", 5 + $v1); // succeeded
  34. $v2 = array(10, 20);
  35. trace("CON3", 5 + $v2[0]); // succeeded
  36. trace("CON4", 5 + array(10, 20)[0]); // succeeded
  37. trace("CON5", 5 + ++$v2[0]); // succeeded
  38. trace("CON6", 5 + $v2[0]--); // succeeded
  39. trace("CON7", 5 + (float)$v1); // succeeded
  40. trace("CON8", 1 << $v1); // succeeded
  41. trace("CON9", $v2[0] == $v2[1]); // succeeded
  42. trace("CON10", $v2[0] = 123); // succeeded
  43. trace("CON11", getValue() - 100); // succeeded
  44. // involving constants
  45. trace("CON21", 1 + CON1); // succeeded;
  46. trace("CON22", 2 * CON2 + CON1); // succeeded;
  47. //*/
  48. ///*
  49. // try to define some constants with names not permitted as tokens
  50. trace("36ABC", 100) . "\n"; // ill-formed name, but no error. Seems to work
  51. trace("#%&", 200) . "\n"; // ill-formed name, but no error. Seems to work
  52. //*/
  53. ///*
  54. // try to redefine a user-defined constant
  55. trace("MY_COLOR", "green"); // Warning, and doesn't change the value
  56. // try to redefine a pre-defined constant
  57. trace("TRUE", 999) . "\n"; // PHP5: No warning, and doesn't change the value, but
  58. // constant("TRUE") and get_defined_constants show 999!
  59. // HHVM: Constant TRUE already defined ...
  60. echo " TRUE's value:" . TRUE . "\n"; // however, this shows the old value, 1
  61. //*/
  62. ///*
  63. // try to define some constants with non-scalar initial values
  64. trace("COLORS", [10, 20]); // Constants may only evaluate to scalar values
  65. class C {}
  66. trace("MY_OBJECT", new C); // Constants may only evaluate to scalar values
  67. $infile = fopen("Testfile.txt", 'r');
  68. if ($infile == FALSE)
  69. {
  70. echo "Can't open file\n";
  71. }
  72. trace("MY_RESOURCE", $infile); // PHP5: Succeeded
  73. // HHVM: Constants may only evaluate to scalar values
  74. trace("MY_RESOURCE", $infile); // PHP5: Duplicate rejected
  75. //*/
  76. ///*
  77. class MyClass
  78. {
  79. // define("DEFINE_INSIDE_CLASS", 10); // not permitted inside a class; OK
  80. // const CON30; // not permitted; OK
  81. // const CON31 = 5 + 10.1 * 3;// failed; unexpected '+', expecting ',' or ';'
  82. // const CON32 = $v1; // failed; unexpected '$v1'
  83. // const CON33 = $v2[0]; // failed
  84. // const CON34 = array(10, 20)[0]; // failed; Arrays are not allowed in class constants
  85. // const CON35 = ++$v2[0]; // failed; unexpected '++'
  86. // const CON37 = (float)10;// failed; unexpected '(float)' const CON38 = 1 << $v1); // surprise? succeeded
  87. // const CON40 = new C; // failed; unexpected 'new'
  88. const CON38 = 99; // succeeded
  89. const CON39 = CON38; // succeeded
  90. }
  91. //*/
  92. ///*
  93. // Note: As opposed to defining constants using define(), constants defined using the
  94. // const keyword must be declared at the top-level scope because they are defined at
  95. // compile-time. This means that they cannot be declared inside functions, loops or
  96. // if statements.
  97. // const CON50; // not permitted; OK
  98. // const CON51 = 5 + 10.1 * 3;// failed; unexpected '+', expecting ',' or ';'
  99. // const CON52 = $v1; // failed; unexpected '$v1'
  100. // const CON53 = $v2[0]; // failed
  101. // const CON54 = array(10, 20)[0]; // failed; Arrays are not allowed in class constants
  102. // const CON55 = ++$v2[0]; // failed; unexpected '++'
  103. // const CON57 = (float)10; // failed; unexpected '(float)' const CON38 = 1 << $v1); // surprise? succeeded
  104. // const CON58 = new C; // failed; unexpected 'new'
  105. const CON59 = 99; // succeeded
  106. const CON60 = CON59; // succeeded
  107. trace("CON61", 321);
  108. const CON62 = CON61; // succeeded
  109. trace("CON63", CON62);
  110. function f($p)
  111. {
  112. // const CON70A = 10; // unexpected 'const'
  113. trace("CON70B", 10); // succeeded
  114. if ($p)
  115. {
  116. // const CON71A = 101; // unexpected 'const'
  117. trace("CON71B", 101); // succeeded
  118. }
  119. }
  120. f(10);
  121. //*/
  122. ///*
  123. // try defining a constant whose name is a keyword
  124. trace("FOR", 100); // succeeded
  125. // echo FOR; // unexpected 'FOR' (T_FOR)
  126. // const FOR = 100; // unexpected 'FOR' (T_FOR)
  127. //*/
  128. class C3
  129. {
  130. const CON1 = 123; // implicitly static, and can't say so explicitly
  131. // public const CON2 = 123; // all class constants are implicitly public; can't say explicitly
  132. // protected const CON3 = 123; // all class constants are implicitly public
  133. // private const CON4 = 123; // all class constants are implicitly public
  134. }
  135. echo "CON1: " . C3::CON1 . "\n"; // use :: notation, as a const is implicitly static
  136. //print_r(get_defined_constants());