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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Access Object methods within seperate function

    Hello fellow coders it's great to be among the best.

    I'm having a little trouble understanding how to access object methods within a new function.

    Example:
    PHP Code:
    //I have initiated a new object:
    $objclass = new class();

    //Ran a method:
    $objclass->someFunction;

    //But now, I want to access this same object and run a few more methods but  within a function:

    function test($var$var2$var3=0){
        
    $objclass->someOtherFunction($var$var2);
    }

    //Continue to access the same object.
    $objclass->anotherFunction
    Whenever I try this I get this error:
    Code:
    Call to a member function someOtherFunction() on a non-object.
    Is what I'm trying to do possible or have I missed something? I have tried passing the entire object into the function but this just spits out errors about how the object is being accessed incorrectly.

    I've done some searches on this but the results seem to apply to Java or C#/C++.

    Any help would be appreciated.

    Thanks,
    Yeshua

    Resolved
    Last edited by yeshuawatso; 03-27-2009 at 08:38 AM.

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    $objclass scope is local to your function, test. Two way to do that:
    - declare $objclass global inside test function
    - pass $objclass as argument to function test.( I prefere that)

    best regards

  • #3
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,849
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    PHP Code:
    function test($var$var2$var3=0){
        
    $objclass->someOtherFunction($var$var2);

    Your $objclass has no scope inside that function. So you'd need to pass this object as the fourth parameter to your function.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #4
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by abduraooft View Post
    Your $objclass has no scope inside that function. So you'd need to pass this object as the fourth parameter to your function.
    This is what I feared I would have to do. Oh well, I can always grit my teeth and debug.

  • #5
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Would passing the object have the same effect if sets and gets are being accessed?

    Example
    PHP Code:
    //I have initiated a new object:
    $objclass = new class();

    //Ran a method:
    $objclass->someFunction->setSomething;

    //But now, I want to access this same object and run a few more methods but  within a function:

    function test($var$var2$var3=0,$objclass){
        
    $objclass->someFunction->setSomethingElse($var$var2);
    }

    //Continue to access the same object.
    $objclass->someFunction->getSomething
    Thanks
    Yeshua

  • #6
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Nevermind, I got it to work as suggested.


  •  

    Posting Permissions

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