note 44841 deleted from function.require-once by aidan

From: Date: Wed, 18 Aug 2004 06:25:08 +0000
Subject: note 44841 deleted from function.require-once by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-74857@lists.php.net to get a copy of this message
Note Submitter: Tolan (tolan at overtops dot org) ---- As far as I can tell this is not entirely true: --- The require_once() statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the require() statement, with the only difference being that if the code from a file has already been included, it will not be included again. See the documentation for require() for more information on how this statement works. --- Take two files: bar.php: <?php class Bar{ function bar(){ require_once('foo.php'); } } $a = new Bar(); $a->Bar(); $b = new Foo(); echo $b->test(); ?> foo.php: <?php class Foo{ function test(){ return "Everything worked."; } } ?> If you execute bar.php, then with require_once() it will parse foo.php as if it had been included or required outside of the method Bar::bar(), allowing you to work with Foo and Bar classes normally. However if you use include() or require() instead of include_once() or require_once(), then you will get the following error (in PHP5): Fatal error: Cannot redeclare class Foo in foo.php on line 3

« previous php.notes (#74857) next »