note 56189 added to function.include
| From: | ystgermain at canac dot com | Date: | Fri, 26 Aug 2005 13:50:26 +0000 |
| Subject: | note 56189 added to function.include | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-94220@lists.php.net to get a copy of this message | ||
When many files need to include the same file then you might end up with an error saying
class/function already defined... To avoid this, you can use the following trick:
Add the following in your include file (NOTE: give a unique defined name for each include file):
if (!defined("GLOBALS_DEFINED"))
{
define("GLOBALS_DEFINED", 1);
//... Place code and definitions here
} // End of if (!defined(...
For example:
-----------------------
globals.php
<?php
if (!defined("GLOBALS_DEFINED"))
{
define("GLOBALS_DEFINED", 1);
class CMessages
{
//... Place code here
} // End of class definition
} // End of if (!defined(...
?>
-----------------------
test.php
<?php
include("globals.php");
//... Place Code here
-----------------------
main.php
<?php
include("test.php");
include("globals.php");
//... Place Code here
?>
-----------------------
----
Manual Page -- http://www.php.net/manual/en/function.include.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+56189
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56189&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56189&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56189&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56189&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56189&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56189&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+56189&report=yes
Search -- http://master.php.net/manage/user-notes.php