note 105323 added to function.return

From: Date: Tue, 09 Aug 2011 14:10:11 +0000
Subject: note 105323 added to function.return
Groups: php.notes 
Request: Send a blank email to php-notes+get-182362@lists.php.net to get a copy of this message
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 ?> ---- Server IP: 81.92.192.129 Probable Submitter: 87.198.254.82 ---- Manual Page -- http://www.php.net/manual/en/function.return.php Edit -- https://master.php.net/note/edit/105323 Del: integrated -- https://master.php.net/note/delete/105323/integrated Del: useless -- https://master.php.net/note/delete/105323/useless Del: bad code -- https://master.php.net/note/delete/105323/bad+code Del: spam -- https://master.php.net/note/delete/105323/spam Del: non-english -- https://master.php.net/note/delete/105323/non-english Del: in docs -- https://master.php.net/note/delete/105323/in+docs Del: other reasons-- https://master.php.net/note/delete/105323 Reject -- https://master.php.net/note/reject/105323 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#182362) next »