phant0m Posted March 1, 2009 Share Posted March 1, 2009 Hello I've got this strange error... Fatal error: Cannot use object of type User as array in ... I don't get this with XAMPP(PHP 5.2.5) but I get it on my remote host(PHP5.2.6) What I do? Well, bascially, I've got this class called "User" and it's got some attribute which is an array. At the end(__destruct) I put it in the session $_SESSION['user'] = $this->user When an instance is created, it checks whether $_SESSION['user'] is set. If it isn't the field is initialized with an empty array in order to be stored into the session at the end. If it is set however, I first assign it to $this->user and then check whether a certain element is empty. This is where the error is triggered. if(isset($_SESSION['user'])){ /* some other code here */ $this->user = $_SESSION['user']; //this is the only place where something is assigned to $this->user if(!empty($this->user['name'])){ It seems as if online, $this->user is a reference to $this, which is really weird. How can it be, that $this->user is a reference to $this on my remote server, yet on my local installation works flawlessly. Also, I used var_dump online to check what $this->user contains immediately before it is assigned to $_SESSION['user']. It contains exactly what I wanted it to: An array with a little information. Something else I've noticed: All the arrays stored in $_SESSION seem to be a reference online: &array(2){...} But "offline", it's like this: array(2){...} Of course, this error doesn't appear when you access the page the first time, because by that time the session is empty and the array is initialized with default values. Any ideas? Thanks in advance. Quote Link to comment Share on other sites More sharing options...
phant0m Posted March 2, 2009 Author Share Posted March 2, 2009 *bump* Quote Link to comment Share on other sites More sharing options...
Mchl Posted March 2, 2009 Share Posted March 2, 2009 You have to use serialize if you want to store object in session AFAIK Quote Link to comment Share on other sites More sharing options...
phant0m Posted March 2, 2009 Author Share Posted March 2, 2009 But I don't want to store the object in the session I wanted it to contain an array, but for some strange reasons, the next time I check the array is gone and an object is there. - But on XAMPP - it doesn't happen. No object whatsoever, the array is always there. ??? Quote Link to comment Share on other sites More sharing options...
Mchl Posted March 2, 2009 Share Posted March 2, 2009 Uh.... Too late in night for me... I see things that are not... Quote Link to comment Share on other sites More sharing options...
trq Posted March 2, 2009 Share Posted March 2, 2009 What versions of php are you using? Quote Link to comment Share on other sites More sharing options...
phant0m Posted March 2, 2009 Author Share Posted March 2, 2009 I don't get this with XAMPP(PHP 5.2.5) but I get it on my remote host(PHP5.2.6) Quote Link to comment Share on other sites More sharing options...
phant0m Posted March 3, 2009 Author Share Posted March 3, 2009 Ok, I have uploaded my site to a different server(Same hosting company) Results: ===Server #1(Remote) Running: PHP 5.2.6 Errors: Fatal Error upon 2nd request due to some SESSION problem, explained above ===Server #2(Remote) Running: PHP 5.2.6 Errors: None - Everything works flawlessly ===Server #3(localhost) Running: PHP 5.2.5 Errors: None - Everything works flawlessly Quote Link to comment Share on other sites More sharing options...
Mchl Posted March 3, 2009 Share Posted March 3, 2009 a workaround would be to check if $this->user is an object before assigning it to session variable... if(!is_object($this->user)) { $_SESSION['user'] = $this->user } Quote Link to comment Share on other sites More sharing options...
phant0m Posted March 3, 2009 Author Share Posted March 3, 2009 Thanks for the input but it doesn't help. The error still occurs. !is_object($this->user) is always validated true, so $this->user is always assigned to $_SESSION['user']. I have searched my entire project to find occurrences of $_SESSION['user'] There are 3. And only in one of these occurrences, something is being assigned to it: $_SESSION['user'] = $this->user; As I had already tested before, $this->user never contains an object when trying to assign it to $_SESSION['users'] But inexplicably, $_SESSION['user'] contains an object when I resume the session on a 2nd request. This is driving me crazy... //edit: Another idea I've had(which was rather unlikely though): Perhaps I assigned something directly to $_SESSION... namely an array containing a key called 'user' which was a reference to that object. I searched for: SESSION\s*= - but it did not return any results Quote Link to comment Share on other sites More sharing options...
phant0m Posted March 3, 2009 Author Share Posted March 3, 2009 I seem to have found the problem: register_globals is set to "On" Thanks a lot for the help anyway. Quote Link to comment Share on other sites More sharing options...
Mchl Posted March 3, 2009 Share Posted March 3, 2009 Huh... I just can't wait until it's removed completely from PHP Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.