Solved

Cannot cast to a user object in PHP

Posted on 2007-11-17
4
1,753 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

734 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