note 44841 added to function.require-once

From: Date: Tue, 17 Aug 2004 20:09:47 +0000
Subject: note 44841 added to function.require-once
Groups: php.notes 
Request: Send a blank email to php-notes+get-74828@lists.php.net to get a copy of this message
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 ---- Manual Page -- http://www.php.net/manual/en/function.require-once.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+44841 Delete -- http://master.php.net/manage/user-notes.php?action=delete+44841&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+44841&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#74828) next »