code_junkie

Force PHP to error on non-declared variables? In objects?

Nov 13th, 2011
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. error_reporting(E_ALL|E_STRICT);
  2. class Joe {
  3. public $lastName;
  4. }
  5.  
  6. $joe = new Joe();
  7. $joe->lastNombre = "Smith";
  8. echo "And here it is " . $jose;
  9.  
  10. <?php
  11.  
  12. error_reporting( E_ALL | E_STRICT );
  13.  
  14. class Joe {
  15. public $lastName;
  16.  
  17. public function __set( $name, $value ) {
  18. if ( !property_exists( $this, $name ) ) {
  19. trigger_error( 'Undefined property via __set(): ' . $name, E_USER_NOTICE );
  20. return null;
  21. }
  22. $this->$name = $value;
  23. }
  24.  
  25. public function __get( $name ) {
  26. if ( !property_exists( $this, $name ) ) {
  27. trigger_error( 'Undefined property via __get(): ' . $name, E_USER_NOTICE );
  28. return null;
  29. }
  30. return $this->$name;
  31. }
  32. }
  33.  
  34. $joe = new Joe();
  35. $joe->lastNom = "Smith";
  36. echo $joe->lastNom , "n";
  37.  
  38. ?>
  39.  
  40. // Reporting E_NOTICE can be good too (to report uninitialized
  41. // variables or catch variable name misspellings ...)
  42. error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
  43.  
  44. error_reporting(E_ALL|E_STRICT);
  45.  
  46. error_reporting(E_ALL);
  47.  
  48. php_error_reporting=5
  49.  
  50. <?php
  51. /**
  52. * Abstract
  53. * Some default Abstract functions that everything should extend
  54. *
  55. * @package example
  56. * @subpackage lib
  57. */
  58.  
  59. /**
  60. * Class mh_abstract
  61. * Some default MH Abstract functions that everything should extend
  62. *
  63. * @package example
  64. * @subpackage lib
  65. */
  66. abstract class lib_abstract
  67. {
  68. /**
  69. * Throws an error if php magic set is called
  70. *
  71. * @param string $key
  72. * @param string|object $value
  73. *
  74. * @throws Exception When trying to set a new class property
  75. */
  76. final public function __set($key, $value)
  77. {
  78. // get if the thing we are trying to set is a object
  79. if(is_object($value))
  80. {
  81. // if so let's just report it's name, not the full object
  82. $value = 'object:'.get_class($value);
  83. }
  84.  
  85. // throw the error!
  86. throw new mh_lib_error_dev_unknownClassProperty('Tried to set new property '.$key.' with value '.$value);
  87. }
  88.  
  89. /**
  90. * Throws an error if php magic get is called
  91. *
  92. * @param string $key
  93. *
  94. * @throws Exception When trying to set a new class property
  95. */
  96. final public function __get($key)
  97. {
  98.  
  99. // throw the error!
  100. throw new mh_lib_error_dev_unknownClassProperty('Tried to get new property '.$key);
  101. }
  102. }
  103.  
  104. function __set($sKey, $sValue)
  105. {
  106. // any setter data should be *returned* here
  107.  
  108. // NOTE: this function will only get called if the property is not
  109. // publicly accessible.
  110. trigger_error("Non-existing property name ".$sKey, E_USER_WARNING);
  111. }
Add Comment
Please, Sign In to add comment