Solved

Cannot cast to a user object in PHP

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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

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…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

626 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