Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    May 2003
    Location
    Toronto, Ontario, Canada
    Posts
    55
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Accessing object fields

    Okay, here's the deal, I store a usersession type object in the $_SESSION var.

    Currently, to use it, I use

    Code:
    $userId = $_SESSION['user']->userId;
    What I wanted to do was write a simply function to return that object.

    Code:
    function getUser() {
       return $_SESSION['user'];
    }
    So that I could then use:

    Code:
    $userId = getUser()->userId;
    however...this doens't work, something about calling a member function on a non object... This is pissing me off, because to me, those 'should' be equivalent statements pretty much.

    If someone can tell me how to get this to work properly, it would be greatly appreciated.

    Pete.
    "God is the history of chance"
    John Gardner

  • #2
    Regular Coder
    Join Date
    Apr 2004
    Location
    Los Angeles
    Posts
    133
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I got the same problem, but it works if you just split
    PHP Code:
    $userId getUser()->userId
    into:
    PHP Code:
    $userId getUser();
    $userId->userId
    Full Code:
    PHP Code:
    <?
    session_start
    ();

    class 
    objFoo {
        var 
    $userId;
        
        function 
    objFoo() {
            
    $this->userId "bar";
        }
    }

    $_SESSION['user'] = new objFoo();

    function 
    getUser() {
       return 
    $_SESSION['user'];
    }

    $userId getUser();
    echo(
    $userId->userId);
    ?>

  • #3
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    PHP Code:
    <?
    function getUser() {
       return 
    $_SESSION['user']->userID;
    }
    ?>
    would probably have done the job ?

    but Hawkmoon's answer illustrates PHP4's lack of decent dereferencing which is AFAIK addressed in PHP5
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #4
    New Coder
    Join Date
    May 2003
    Location
    Toronto, Ontario, Canada
    Posts
    55
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well that would work, if all I ever wanted was the user's Id. However, this is a big object, and I just wanted a convenient way of getting at all it's fields/methods.

    Thanks for the replies though.
    "God is the history of chance"
    John Gardner

  • #5
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    you can use a generic getter , though there are debatable reasons some may suggest that makes this a bad idea ... but anyway (e.g. I disagree)

    PHP Code:
    <?
    function get_user_prop$property ){
        if( isset( 
    $_SESSION['user']->$property ) ){
            return 
    $_SESSION['user']->$property ;
        }
        return 
    false ;
    }
    $userId get_user_prop'userId' ) ;
    ?>
    Last edited by firepages; 04-04-2004 at 04:20 AM.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •