Solved

Cannot cast to a user object in PHP

Posted on 2007-11-17
4
1,742 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

708 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now