Solved

Cannot cast to a user object in PHP

Posted on 2007-11-17
4
1,751 Views
Last Modified: 2012-08-13
Hi,

I’ve got a PHP class called User which I am using across sessions by implementing the __sleep and __wakeup methods.  The class itself is working fine, but I’ve lost introspection within the visual studio IDE as I have to get it from the session object and not via the new object keyword.

So when I do $user = new User(); code completion works fine as the IDE knows it’s a User object.

However, when I do $user = $_SESSION['user']; the IDE doesn’t know what type it is and I lose the code completion.

Does anyone know how I could get around this?

Regards

Graham
0
Comment
Question by:grahamtilson
  • 2
  • 2
4 Comments
 
LVL 21

Expert Comment

by:nizsmo
ID: 20305444
I wouldn't think you can get around this. This is because:

$_SESSION["user"] you know will be a User() object, however it has the potential to be anything else, string, integer, etc... thus the IDE cannot be sure therefore doesn't auto-complete for you.

You can always try casting, if this doesn't work then I wouldn't think there's a way to fix this.

$user = (User) $_SESSION['user'];

Hope this helps.
0
 

Author Comment

by:grahamtilson
ID: 20311289
Yes, I tried casting, but you can't cast to a user object, which seems a bit crazy.  I think I'll raise this with Zend as a support ticket.
0
 

Accepted Solution

by:
grahamtilson earned 0 total points
ID: 20311749
I got a response from Zend.  The answer is PHPDOC!

This was the solution:

**
* Returns the user object
*
* The user object contains all data about the user including the signed on flag
*
* @return       User      Returns the User object
*/
function get_user_obj() {
      if (!isset($_SESSION['user'])) {
            $_SESSION['user'] = new User();
      }
      return $_SESSION['user'];
}

$user = get_user_obj();

Now code completion with work for $user
0
 
LVL 21

Expert Comment

by:nizsmo
ID: 20314264
ahh i see, nice one thanks for sharing the solution :)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question