views:

90

answers:

1

Hi, 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

+2  A: 

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...)


For more informations, you can read the Using Register Globals 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 :

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)

Pascal MARTIN
Cool! thanks a lot that solved my problem I changed test in $_SESSION and it work now!!
Bassel Safadi
I guess that's a solution too -- but don't forget you might run into the same kind of trouble again, for other variables !;;; YOu're welcome :-)
Pascal MARTIN
yes you are right, but it's not possible to ask everybody to disable register_globals for this to work !!! thanks again
Bassel Safadi
Make it possible or wait for this house of cards to come crashing down
symcbean