note 105323 deleted from function.return by danbrown

From: Date: Tue, 09 Aug 2011 14:18:18 +0000
Subject: note 105323 deleted from function.return by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-182363@lists.php.net to get a copy of this message
Note Submitter: chris at willowsconsulting dot ie Reason: in docs ---- FILE: file.php =============================== <?php // some code return 3; ?> =============================== Then this FILE: class.php =============================== <?php class myclass.php{ public function doThis(){ require_once("file.php"); echo "Will this being displayed?"; } // end of: private function doThis(){ } // end of: class myclass.php{ $newClass= new myclass(); $newClass->doThis(); ?> =============================== You will expect that nothing will be displayed right?? Unfortunately: <?php echo "Will this being displayed?"; ?> will BE executed! The "return 3;" in file.php will just exit the execution in file.php and will return into doThis(); function !!... The solution is: <?php class myclass.php{ public function doThis(){ $r=require_once("file.php"); if ($r===3) return 3; echo "Will this being displayed?"; } // end of: private function doThis(){ } // end of: class myclass.php{ $newClass= new myclass(); $newClass->doThis(); ?> Yes: require_once will/can return a value! I used the value 3 to be sure it's unique (if there are other returns in file.php). Note that this is incorrect and will trigger a fatal error: <?php if (require_once("file.php")===3) return 3; // fatal error ?>

« previous php.notes (#182363) next »