baman Posted October 29, 2009 Share Posted October 29, 2009 Hello. I am learning OO with PHP and have hit a problem. Some code runs as perfectly valid code, where i would like PHP to issue a warning / error. I guess this is because of the loose typing of PHP, however this cause real bugs go unnoticed in my code and i am looking for a way to be able to detect this. The code sample does not produce any warning/error using "error_reporting=E_ALL" with php 5.2.10 Do anyone have a idea or suggestion? class Objekt { var $foo; var $bar; } $x = new Objekt(); $x->foo = 4; var_dump($x); $x->baz = 9; //i would like this to cause an warning because "baz" is a undefined property of the Objekt class. but it dont and "baz" is added to the object var_dump($x); Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted October 29, 2009 Share Posted October 29, 2009 Please note: requires php >= 5.1 <?php $x = new Objekt(); if(property_exists($x, 'baz')) { $x->baz = 9; } else { print "property does not exist"; } ?> Quote Link to comment Share on other sites More sharing options...
Mark Baker Posted October 29, 2009 Share Posted October 29, 2009 In PHP5 you could probably do something with magic getters and setters class Objekt { public $foo; public $bar; public function __set($name, $value) { if (!isset($this->$$name)) { throw new Exception($name.' property does not exist'); } } } $x = new Objekt(); $x->foo = 4; var_dump($x); $x->baz = 9; var_dump($x); Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted October 29, 2009 Share Posted October 29, 2009 absolutely Quote Link to comment Share on other sites More sharing options...
baman Posted October 29, 2009 Author Share Posted October 29, 2009 Mark: Thank you a ton! By overriding the internal setter with __set() like you suggest, i now can control this stuff as I wanted! Niel: It was avoiding adding extra checks (like property_exists() ) that was my goal, but thanks for your reply Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted October 29, 2009 Share Posted October 29, 2009 Use the same for your getter. Make sure you handle your exceptions gracefully. Quote Link to comment Share on other sites More sharing options...
baman Posted November 21, 2009 Author Share Posted November 21, 2009 I figured out that you will get a warning on the above code by configuring php.ini like this: error_reporting = E_ALL | E_STRICT Notice: Undefined property: Objekt::$paz in /devel/web/obj_bugtest.php on line 30 Quote Link to comment Share on other sites More sharing options...
Mark Baker Posted November 21, 2009 Share Posted November 21, 2009 So what's line 30? Quote Link to comment Share on other sites More sharing options...
baman Posted December 2, 2009 Author Share Posted December 2, 2009 So what's line 30? on line 30 is the invalid access of a undefined class property, see first post Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.