__autoload

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Dave2000
Forum Contributor
Posts: 126
Joined: Wed Jun 21, 2006 1:48 pm

__autoload

Post 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 :)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

anywhere on the global scope. PHP will automatically call it should it not find the class within the current scope.
Dave2000
Forum Contributor
Posts: 126
Joined: Wed Jun 21, 2006 1:48 pm

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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";
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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(); ?>
Dave2000
Forum Contributor
Posts: 126
Joined: Wed Jun 21, 2006 1:48 pm

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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?
Dave2000
Forum Contributor
Posts: 126
Joined: Wed Jun 21, 2006 1:48 pm

Post 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?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
Post Reply