I wrote a class to handle user authentication and session variable creation for an enterprise-scale web app.

In a nutshell, once the user is authenticated, we pull their metadata out of the MySQL db and assign each field as an object of the class....

something like this:

$user->email = $row->email_address;

Next, I use another method to assign these objects to session variables, like:

$_SESSION[email] = $user->email;

The problem I'm having is that when trying to access the session vars in the method of another class, I am getting this error:

So, attempting something like this in a class called "email"...

  $from = "FROM: " . $_SESSION[email] . "\r\n";

...results in this....

Catchable fatal error: Object of class __PHP_incomplete_class could not be converted to string in ...

So, what I'm thinking is that in my user class, when I assign the user objects to session vars, I need to invoke the __toString() function for each.

My question here is how do I write a __toString() function to handle multiple variables? The ONLY examples I've seen online return only one value, like this:

        public function __toString() {
                return $this->content;
        }

I guess the simplest thing to do would be to assign the query objects directly to session vars instead of first assigning them to class objects. I'd rather get it working correctly though.

    Do you include the class definition on the page before starting the session on that page?

    Without that definition it can't properly deserialise the object.

      Include the User class? No, because that class is invoked only once, at login, when the session vars are created. After that, throughout the rest of the application, I just access the session vars rather than instantiating the class.

      Let me explain what I'm doing a bit further.

      So I have a second class called "email" which handles sending out mail() for the application, and also logs metadata about each email to a .txt file.

      Inside that class's methods, I access $SESSION[email] and $SESSION[username] in a couple of places, for things such as the "from" address for an email being sent and to idenfity the user in the email log.

      Both of these methods work fine ONCE. After that, they throw the error. This is odd, because if you log out and log back in (blow away the session vars and then recreate them), it works again, once.

      I've isolated the issue to these session vars and not an object inside the email class.

        Chris 96 WS6 wrote:

        Include the User class? No,

        Well, there's your problem.

        Weedpacket wrote:

        Without that definition it can't properly deserialise the object.

        Only the object's properties are serialised, not any of its methods - and that includes __toString().

          Chris 96 WS6 wrote:

          Include the User class? No,

          Well, there's your problem.

          Weedpacket wrote:

          Without that definition it can't properly deserialise the object.

          Only the object's properties are serialised, not any of its methods - and that includes __toString().

            I've implemented your suggestion after doing a little research (and therefore now understanding why you should include the class), but it hasn't fixed the issue.

            What's driving me nuts is this problem didn't exist in my development environment (ZendCore on Windows Server), its only evident on our test and production environments.

              OK, I think I'm finally getting to the bottom of this.

              This is an object, right?

              $user = new User();

              This is a property of an object, right?

              $user->email;

              So why am I getting the error about converting an object into a string when my function is acting on a PROPERTY and not the whole object?

              Why isn't this valid?

              $string = $user->email;

              I'm not putting OBJECTS into session vars, I'm putting object PROPERTIES into session vars.

                I think its fixed.....looks like it was a namespace issue.

                I'm not sure what environment variable is set in php.ini to cause it to do this, but on our test & production servers, "$email" is treated the same as "$_SESSION[email]";

                The instance of my email class was "$email = new Email();"

                That was conflicting with the email session var.

                So I renamed the class and used a different object variable, and it seems to be working now.

                  Chris 96 WS6 wrote:

                  I'm not sure what environment variable is set in php.ini to cause it to do this,

                  Sounds like the PHP 4.2 compatibility bug.

                  ; PHP 4.2 and less have an undocumented feature/bug that allows you to
                  ; to initialize a session variable in the global scope, albeit register_globals
                  ; is disabled.  PHP 4.3 and later will warn you, if this feature is used.
                  ; You can disable the feature and the warning separately. At this time,
                  ; the warning is only displayed, if bug_compat_42 is enabled.
                  
                  session.bug_compat_42 = 0
                  session.bug_compat_warn = 1
                  

                  You might have bug_compat_42 turned on and bug_compat_warn turned off (what I've posted is the opposite - the default).

                    2 months later

                    Posts #10 and #11 should be moved to "Class Inheritance Programming problem." right?

                      Write a Reply...