Jump to content

[SOLVED] Why won't this class blooming load?


sennetta

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/87907-solved-why-wont-this-class-blooming-load/
Share on other sites

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.

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';

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.