3

So, I have xampp. I tested this code on ZendServer, result the same.

<?php
error_reporting(E_ALL);

define ("ABS_PATH", 'C:\xampp\htdocs\oopHotLine\\');

function __autoload($class_name) {
    echo 'gg';
    require_once (ABS_PATH.'classes\\'.$class_name.'.php');
}

$process=new Main('1');
?>

after php.exe -a index.php i have this:

Interactive mode enabled
Fatal error: Class 'Main' not found in C:\xampp\htdocs\oopHotLine\index.php on line 10
[Finished]

so, it doesn shows 'gg' output. If i manually do __autoload('Main'); - all OK. The same for the manual require_once or include. All permissions in windows folder set to full access. Php version - PHP Version => 5.3.8;

Please, help.

4
  • Are your sure the file is called? Commented Oct 15, 2011 at 22:20
  • When you load a custom function like the following with spl_autoload_register, does it work then? Commented Oct 15, 2011 at 22:25
  • Yes, i am quite sure, and, even if not - shouldn't it show 'gg' at first, before the error? Commented Oct 15, 2011 at 22:26
  • custom loader with spl_autoload_register have the same output Commented Oct 15, 2011 at 22:28

3 Answers 3

2

As DaveRandom pointed:

Autoloading is not available if using PHP in CLI interactive mode.

So, instead of using php.exe -a index.php, cut the -a off and try running the script with php.exe index.php instead.

Sign up to request clarification or add additional context in comments.

Comments

0

Notes from the PHP Manual on this subject, that may be relevant:

Judging by the first message logged, this is probably the answer:

Autoloading is not available if using PHP in CLI interactive mode.

...but also:

spl_autoload_register() provides a more flexible alternative for autoloading classes. For this reason, using __autoload() is discouraged and may be deprecated or removed in the future.

Aside from this, here is how I would write your autoload function:

function __autoload($class_name) {
  if (file_exists(ABS_PATH."classes/$class_name.php")) { // At least check the file exists before you require it!
    // Forward slashes work on Windows too (in PHP at least)... and they make it more portable
    require_once (ABS_PATH."classes/$class_name.php");
  }
}

3 Comments

<?php error_reporting(E_ALL); define ("ABS_PATH", 'C:/xampp/htdocs/oopHotLine/'); function autoload ($class_name) { echo 'gg'; if (file_exists(ABS_PATH."classes/".$class_name.".php")) { require_once (ABS_PATH.'classes/'.$class_name.'.php'); } } spl_autoload_register ('autoload'); $process=new Main('1'); ?> gives the same output, sorry for formatting.
@NiPh I really think this is more to do with Autoloading is not available if using PHP in CLI interactive mode....
Thank you for your help and information about future __autoload deprecation.
0

http://www.php.net/manual/en/function.spl-autoload-register.php

If your code has an existing __autoload function then this function must be explicitly registered on the __autoload stack. This is because spl_autoload_register() will effectively replace the engine cache for the __autoload function by either spl_autoload() or spl_autoload_call().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.