-1

I have a weird error in my php code. I'm using spl_autoload_register this way:

function load($class) {
    require 'class/' . $class . '.php';
}
spl_autoload_register('load');

Then on my page, when I try loading a class, the whole page is loaded again. This is what I write:

<?php include('inc/header.php'); ?>
<body>
<?php include('inc/nav.php'); ?>
[some html]
<?php load('Class'); ?>
[otherhtml]
<?php include('inc/footer.php') ?>

But when I try running it on my local server (using xampp), the whole page is included again, and it looks like this:

[header]
<body>
[nav]
[some html]
    [header]
    <body>
    [nav]
    [some html]
    [other html]
    [footer]
[other html]
[footer]

And I get a few php errors, mostly due to the fact that the header is included twice:

A session had already been started - ignoring session_start().

And

Fatal error: Cannot redeclare load() (previously declared in C:...inc\header.php:2) in C:...inc\header.php on line 4

This only happens when running on xampp. I uploaded everything to my webserver, and there is no problem. It was working fine two days ago, and might have started when I tried installing composer using phpstorm.

Any help would be appreciated. Thanks !

ChypRiotE
  • 285
  • 4
  • 11
  • 1
    The excerpts are insufficient to guess where you loaded something twice. It's unclear why your `load` function is declared in header anyway, or where the `session_start` call resides, and what `utils.php` does. – mario Jan 21 '15 at 18:13
  • Yeah, it's kinda hard to post a short version of my code since I have no clue where the problem is. Would it be better if I added a link to a git with the whole thing ? – ChypRiotE Jan 21 '15 at 18:15
  • Lengthy code listings or a complete file dump would turn this more into a debugging question. If you can't explain the code flow, then use a debugger first to follow it yourself, or provide a [xdebug/webgrind](http://stackoverflow.com/questions/1235513/php-call-graph-utility) or [kcachegrind call graph](http://kcachegrind.sourceforge.net/html/Home.html) etc. – mario Jan 21 '15 at 18:21

1 Answers1

1

The advantage of spl_autoload_register is that there is no need to call a function to include class XY as the registered autoloader will be triggered if one instantiate class XY but has not been declared (included) yet.

In the code above you first declare the load function, register it and later on you call the load function.

This is your code:

<?php include('inc/header.php'); ?>
<body>
<?php include('inc/nav.php'); ?>
[some html]
<?php load('Class'); ?>
[otherhtml]
<?php include('inc/footer.php') ?>

But when using spl_autoload_register I would use following:

<?php include('inc/header.php'); ?>
<body>
<?php include('inc/nav.php'); ?>
[some html]
new Load();
[otherhtml]
<?php include('inc/footer.php') ?>

The difference is in line 5.

About the two errors you are having: I fully agree to mario's reply.

Alexander
  • 501
  • 1
  • 6
  • 16
  • Thanks, I hadn't understood the point of spl_autoload_register, and thought it was just a way of overloading the load function or something like that. Removed every occurence of load() in my code and it worked. Thanks a lot ! – ChypRiotE Jan 21 '15 at 19:37