Reputation: 487
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
Upvotes: 0
Views: 538
Reputation: 401132
Maybe you have some variable named test
in $_SESSION
, and have register_globals
enabled ?
In which case, the $_SESSION['test']
variable will be created as a global $test
variable by the call to session_start()
, overridding any existing $test
variable of your script.
This would also explain why this is happening on some servers and not some others : register_globals
is Off
by default -- and has been for many years, but some hosts keep it enabled :-(
(When people say register_globals
is evil, it's not without a good reason...)
session_start();
$test = new test();
$test->load("email");
This way, even if a $test
is created because of register_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_globals
Off : 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)
Upvotes: 3