SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Enthusiast minus's Avatar
    Join Date
    Jan 2002
    Location
    Pittsburgh, PA, USA
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Access to REGISTERED VARIABLE through an OBJECT method

    Hello,

    I am having trouble accessing a registered variable from within an object (well, I actually just can't...). I couldn't find anything official that would confirm such a problem.

    While I m trying to find another way around, I would grealty welcome any experienced developer's tip on that !

    Thanks a bunch.
    MinuS

  2. #2
    gingham dress, army boots... silver trophy redux's Avatar
    Join Date
    Apr 2002
    Location
    Salford / Manchester / UK
    Posts
    4,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i might be wrong, but this sounds like a variable scope problem to me. try explicitly setting your variable to global in your object's code before you try accessing it.
    PHP Code:
    global $whatever
    re·dux (adj.): brought back; returned. used postpositively
    [latin : re-, re- + dux, leader; see duke.]
    WaSP Accessibility Task Force Member
    splintered.co.uk | photographia.co.uk | redux.deviantart.com

  3. #3
    SitePoint Enthusiast minus's Avatar
    Join Date
    Jan 2002
    Location
    Pittsburgh, PA, USA
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I was referring to a registered variable, which, I assume, is supposed to be global already (am I wrong?). Anyways, I tried to "re-declare" the variable as you suggested. Doesn't work .

  4. #4
    gingham dress, army boots... silver trophy redux's Avatar
    Join Date
    Apr 2002
    Location
    Salford / Manchester / UK
    Posts
    4,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it might help if you actually posted some code
    re·dux (adj.): brought back; returned. used postpositively
    [latin : re-, re- + dux, leader; see duke.]
    WaSP Accessibility Task Force Member
    splintered.co.uk | photographia.co.uk | redux.deviantart.com

  5. #5
    Super Ninja Monkey Travis's Avatar
    Join Date
    Dec 2001
    Location
    Sioux City, Iowa
    Posts
    691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    heres a quick example of how to do this
    PHP Code:
    class MyClass {
        var 
    $my_var;

        function 
    SetMy_var ($my_var) {
            
    $this->my_var $my_var;
        }
    }
    $class = new MyClass;
    $class->SetMy_var($my_var); 
    Last edited by Trav; May 29, 2002 at 18:25.
    Travis Watkins - Hyperactive Coder
    My Blog: Realist Anew
    Projects: Alacarte - Gnome Menu Editor

  6. #6
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Trav
    heres a quick example
    PHP Code:
    class MyClass {
        var 
    $my_var;

        function 
    SetMy_var ($my_var) {
            
    $this->my_var $my_var;
        }
    }
    $class = new MyClass;
    $class->SetMy_var($my_var); 
    And the problem is?

  7. #7
    Super Ninja Monkey Travis's Avatar
    Join Date
    Dec 2001
    Location
    Sioux City, Iowa
    Posts
    691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i didn't start the topic so its not my problem. i was fixing it for him
    Travis Watkins - Hyperactive Coder
    My Blog: Realist Anew
    Projects: Alacarte - Gnome Menu Editor

  8. #8
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Trav
    i didn't start the topic so its not my problem. i was fixing it for him
    Ohh I see

    I would add this
    PHP Code:
    class MyClass {
        var 
    $my_var;

        function 
    MyClass() {
          
    // initialize $this->my_var, ie, $this->my_var = '' or $this->my_var = array(), etc.
        
    }

        function 
    SetMy_var ($my_var) {
            
    $this->my_var $my_var;
        }
    }
    $class = new MyClass();
    $class->SetMy_var($my_var); 

  9. #9
    Super Ninja Monkey Travis's Avatar
    Join Date
    Dec 2001
    Location
    Sioux City, Iowa
    Posts
    691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    why not just
    PHP Code:
    var $my_var '';

    //or

    var $my_var = array(); 
    ?
    Last edited by Trav; May 29, 2002 at 19:07.
    Travis Watkins - Hyperactive Coder
    My Blog: Realist Anew
    Projects: Alacarte - Gnome Menu Editor

  10. #10
    SitePoint Enthusiast minus's Avatar
    Join Date
    Jan 2002
    Location
    Pittsburgh, PA, USA
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys for your help. I had actually figured that I d have to proceed somehow like that, getting the variable as an attribute to the class.

    I guess you confirm that a registered variable can't be accessed through an object as is.

    Talk to you next post!

    MinuS

  11. #11
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Trav
    why not just
    PHP Code:
    var $my_var '';

    //or

    var $my_var = array(); 
    ?
    Yes, you can do that but I prefer set up all the variables default values inside the constructor, that way is less messy.
    Paul

  12. #12
    Super Ninja Monkey Travis's Avatar
    Join Date
    Dec 2001
    Location
    Sioux City, Iowa
    Posts
    691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Paul S
    Yes, you can do that but I prefer set up all the variables default values inside the constructor, that way is less messy.
    In my opinion it is less messy to do it the way I showed because you can see that the variable is going to be used as an array or whatnot without digging for it in the methods of the class.
    Last edited by Trav; May 30, 2002 at 09:14.
    Travis Watkins - Hyperactive Coder
    My Blog: Realist Anew
    Projects: Alacarte - Gnome Menu Editor


Bookmarks

Posting Permissions

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