I have this class that have a function to load other classes and create instances.
if you used this class like this:
$test = new test();
$test->load("email");
it works perfectly as expected
but when using session_start();
$test = new test();
session_start();
$test->load("email");
an error is created and nothing else is there:
PHP Fatal error: Call to a member function load() on a non-object in bla bla bla
the class used with session_start:
<?php
class test
{
function load($class){
static $objects = array();
if (isset($objects[$class]))
{
return $objects[$class];
}
require('libraries/'.$class.'.php');
$name = 'ext_'.$class;
$objects[$class] =& new $name();
$this->$class = $objects[$class];
return $objects[$class];
}
}
$test = new test();
session_start();
$test->load("email");
?>
and here is libraries/email.php:
<?php
class ext_email
{
function ext_email(){
echo "email is working";
}
}
?>
can you please advice what is wrong with this? a way to improve the load function?
this thing works on some installations of apache and fail to work on others. depending on some configs that I don’t know what exactly is it..
I want to be able to do the following:
$test = new test();
session_start();
$test->load("email");
thanks a lot in advance
Maybe you have some variable named
testin$_SESSION, and haveregister_globalsenabled ?In which case, the
$_SESSION['test']variable will be created as a global$testvariable by the call tosession_start(), overridding any existing$testvariable of your script.This would also explain why this is happening on some servers and not some others :
register_globalsisOffby default — and has been for many years, but some hosts keep it enabled 🙁(When people say
register_globalsis evil, it’s not without a good reason…)For more informations, you can read the [Using Register Globals][2] page of the manual — there is even a paragraph about `$_SESSION` and some problems that `register_globals` can cause.
Now, on how to fix this… Well, I suppose the fastest way would be to make sure that `session_start()` is called before you set `$test` to what you want it to be :
This way, even if a
$testis created because ofregister_globals, your variable will override it — and the last one is the one that’s right ^^But the best solution would be to turn
register_globalsOff : that’s a reliquate from the past… That should probably never have existed 🙁(There are some bad things in PHP ; that’s one of them, in my opinion)