On 02/21/2012 03:18 PM, Ralf Lang wrote:
In PHP I would rather do $mother = $baby->getMother(); if ($mother) { $granny = $mother->getMother(); if ($granny) { $granny_name = $granny->getName(); } } because I have no way to catch if $mother or $granny cannot be retrieved.
There is a way: 1 <?php 2 class NonCallable { 3 public function __call($meth, $args) { 4 throw new Exception("$meth does not exist"); 5 } 6 } 7 8 class Foo { 9 protected $bar; 10 public function __construct($bar=NULL) { 11 $this->bar = $bar; 12 } 13 public function bar() { 14 if($this->bar) { 15 return $this; 16 } 17 return new NonCallable; 18 } 19 } 20 21 class Bar { 22 public function bark() { 23 echo 'bark'; 24 return $this; 25 } 26 } 27 28 $foo = new Foo; 29 30 try { 31 $foo->bar()->bark(); 32 } catch(Exception $e) { 33 echo 'something went wrong'; 34 return 1; 35 } 36 Liebe Grüße, Flavius -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php