Page 1 of 1

__autoload

Posted: Mon Jul 16, 2007 6:46 pm
by Dave2000
Hi,

I understand how the __autoload function works. *Very* basically, something like...

Code: Select all

__autoload($class)
{
	require_once $classclass. '.php';
	return;
}
However, I cannot for the life of me work out where I am supposed to place this function??? I am unable get it to work in my scripts. I have looked all over the PHP manual - cant find anything about where this function is meant to go :?

Thanks in advance,

Shears :)

Posted: Mon Jul 16, 2007 7:05 pm
by Jenk
anywhere on the global scope. PHP will automatically call it should it not find the class within the current scope.

Posted: Tue Jul 17, 2007 7:14 am
by Dave2000
Thanks for your reply Jenk :) Do you mean that I have to write the autoload function at the top of every page that I wish to use it in? I still cant get it to work. *sigh* What am I doing wrong here please...

test_class.php

Code: Select all

<?php
error_reporting(E_ALL);

class test_class
{
	public function __construct()
	{
		Echo "loaded";
		return;
	}
}

?>
test.php

Code: Select all

<?php
error_reporting(E_ALL);

Echo "hello";

function __autoload($class_name)
{
	if (!file_exists($class_name . '.php') { Echo 'file cant be found'; }
	
	require_once $class_name . '.php';
}

new test_class();

?>
Both files are in the same directory.

I get no errors message in test.php - just a white screen. I get no 'file cant be found message'. Incidentally, the "hello" at the top of test.php does not display unless I completely remove the autoload function. :? What am I doing wrong please? Thanks in advance!

Shears

Posted: Tue Jul 17, 2007 7:20 am
by Chris Corbyn

Code: Select all

ini_set("display_errors", true);
Are you sure the class file is in the same directory as the script which is using it? Includes are relevant to the script loaded in the browser, not to the file which does the include. If you need an include relative to the current file itself:

Code: Select all

include dirname(__FILE__) . "/relative/to/here.php";

Posted: Tue Jul 17, 2007 7:28 am
by volka
Shears wrote:I get no 'file cant be found message'. Incidentally, the "hello" at the top of test.php does not display unless I completely remove the autoload function.
That's because you have a syntax error in the autoload function code.
Unless you're trying this on a production server please set

Code: Select all

error_reporting = E_ALL
display_errors = On
display_startup_errors = On
in your php.ini. Those parameters are already in the php.ini, just change their values.
Then restart the webserver and check the values via

Code: Select all

<?php phpinfo(); ?>

Posted: Tue Jul 17, 2007 8:28 am
by Dave2000
I set those values in php.ini and I see the error now.. oops :oops: Thank you! :)

So I have to write/include/require an autoload function in every file that uses it? 8O

Posted: Tue Jul 17, 2007 8:30 am
by volka
Shears wrote:So I have to write/include/require an autoload function in every file that uses it? 8O
How did you come to this conclusion?

Posted: Tue Jul 17, 2007 8:55 am
by Dave2000
(Sorry. That was a separate point. I probably shouldn't have used the word "So".)

For example, I have the class test_class in test_class.php . If i want to use this class in test1.php, test2.php, test3.php test4.php, it seems I need to write the autoload function in those four files?

Posted: Tue Jul 17, 2007 9:16 am
by volka
When your webserver receives a http request for a php script it creates a new instance of php. This instance parses the script that was requested and starts executing it. If this script includes other script via include/require the source code is "imported" to the same php instance. A function definition is valid for the whole php instance. If the single request is handled the webserver discards the php instance.
You have to define the __autoload() function for each instance of php where you want to use it - that is not necessarily for each single file.