sennetta Posted January 26, 2008 Share Posted January 26, 2008 I have a form handling script which sets the results of a form in a MySQL database. I am using a class to push data onto the database, and I have used this many times before without any problems. The problem I appear to be having is loading and instantiating the class. Classes are held in "lib/" directory and are called "ClassName.php". Script is in root directory and and creates objects using an autoload function: function __autoload($classname) { // generate file path $filePath = "lib/".$classname.".php"; if (file_exists($filePath)) { echo $filePath; require_once($filePath) or die("class not found"); } } Trying to load just one class called AddData: $dataIn = new AddData(); This outputs the following: lib/AddData.php Warning: require_once(1) [function.require-once]: failed to open stream: No such file or directory in D:\www\sarah\feedback.php on line 71 Fatal error: require_once() [function.require]: Failed opening required '1' (include_path='.;C:\php5\pear') in D:\www\sarah\feedback.php on line 71 According to the line "require_once(1)" the class is being renamed "1"... This has been bugging me for hours now, and I can't find what's wrong.... Can anyone help? Cheers, Anthony Quote Link to comment https://forums.phpfreaks.com/topic/87907-solved-why-wont-this-class-blooming-load/ Share on other sites More sharing options...
448191 Posted January 26, 2008 Share Posted January 26, 2008 It's this line: require_once($filePath) or die("class not found"); require_once is not function, it doesn't return anything. Also it will throw a fatal error when the file is not found, so 'or die()' is obsolete. Why this line results in require_once(1) is beyond me though. Quote Link to comment https://forums.phpfreaks.com/topic/87907-solved-why-wont-this-class-blooming-load/#findComment-449780 Share on other sites More sharing options...
mem0ri Posted January 27, 2008 Share Posted January 27, 2008 448191 is correct in stating that you don't need an "or die()" with require_once(), but I would venture to say that your $filePath is somehow incorrect inside the __autoload(). Try something like: $filePath = $_SERVER['DOCUMENT_ROOT'].'/lib/'.$classname.'.php'; Quote Link to comment https://forums.phpfreaks.com/topic/87907-solved-why-wont-this-class-blooming-load/#findComment-450713 Share on other sites More sharing options...
sennetta Posted February 3, 2008 Author Share Posted February 3, 2008 Thanks for your pointers on require_once. Still no idea what was going on with the $classname argument apparently changing, but I had to go a different route anyways as the server I had to use only had php4 installed. Cheers Edit: marked as solved for tidiness Quote Link to comment https://forums.phpfreaks.com/topic/87907-solved-why-wont-this-class-blooming-load/#findComment-456900 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.