note 38737 added to function.require-once

From: Date: Mon, 05 Jan 2004 13:33:32 +0000
Subject: note 38737 added to function.require-once
Groups: php.notes 
Request: Send a blank email to php-notes+get-62943@lists.php.net to get a copy of this message
In the following example require_once does not work (PHP version 4.3.3): Class definition: <?php class aname { function aname() { require_once('anotherclass.php'); $anotherclassobj=new anotherclass; } } ?> Main page: <?php require_once('anotherclass.php'); include('anameclass.php'); $anotherclassobj=new anotherclass; $anameobj=new aname; ?> The error I get is when I create a new aname object in the main page. Error is "cannot redeclare class anotherclass in anotherclass.php". You have to use class_exists in the definition of aname class to get past this problem. I.E: <?php class aname { function aname() { if(!class_exists('anotherclass')) { require_once('anotherclass.php'); } $anotherclassobj=new anotherclass; } } ?> I hope this helps someone. :) Kind Regards Anders Mårtensson ---- Manual Page -- http://www.php.net/manual/en/function.require-once.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+38737 Delete -- http://master.php.net/manage/user-notes.php?action=delete+38737&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+38737&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#62943) next »