Solved

Cannot cast to a user object in PHP

Posted on 2007-11-17
4
1,750 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
What does != "" mean in programming 8 78
Moving from Mcrypt to OpenSSL 18 45
What if i make webbased alternative for MS Access 5 67
if (is_singular not working 5 18
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

839 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