>From: "Brian Moon" <[EMAIL PROTECTED]> > > --- Start --- > > > > class Something > > { > > public function __construct() > > { > > // Oops, forgot to initialise $this->something... > > } > > > > public function f() > > { > > return $this->something; > > } > > > > private $something; > > } > > > > > > error_reporting(E_ALL); > > > > $something=new Something; > > > > echo $something->f()+10; // Prints "10". > > If you can't trust the return values of your methods, I would use: > > $var = $something->f(); > > if($var!==NULL){ > echo $var+10; // Prints "10". > }
Right... And you can always use return codes, instead of exceptions or trigger_error(). The problem is that there's nothing that _enforces_ this checking, and since it adds verbosity, it's typically not done. If you don't believe me have a look at some of all of the C code out there, which typically to a large degree don't check return values (and it shows... Programs segfaulting and the like, if something unexpected happens), as it makes the program convoluted and messy, and is easily forgotten. It's for reasons like this that exceptions (and possibly trigger_error()) were invented in the first place. It's similar with the example above: Type-checked return values means you don't have to write all these tedious manual checks, just to ensure program correctness (and had the return type hint been mandatory, there's no way you could forget it or not bother with it, either).. > However, its crap like this that reminds me why I don't use PHP OOP for > all my code. I can find no non-OOP code that behaves this way. The above was a contrived example, meant to illustrate the point. What's so bad about it? That it doesn't check the return value? Well... having to check the return value, to ensure things didn't go wrong is something I left a looong time ago, when I was programming C, before I started with C++ and Java, which gives better ways of handling this, and have only had to take it up again, in PHP. > Even my > favorite PHP trick, using settype() to initialize vars, does not set the > var to NULL. settype() is still a manual operation - there's no way to automatically guarantee initialisation. In a language like C++, the class members are default-initialised, unless you explicitly initialise them, so there's no way you can forget it or ignore it. The result: More robust programs. Regards, Terje -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php