The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

Variable Scope issue passing in Global Vars

Kane Cochran's Avatar

Kane Cochran

23 Jul, 2012 04:35 PM

This might be a silly question, but its had me stumped. I have a global variable that I need passed into a few of my routes and I can't figure out how to pass it ($appID) into the Round route correctly. Here's my setup so far

// Global App ID $appID = 1029jah324

// Create the Slim Framework instance $app = new Slim();

$app->get('/rounds/:id', 'getRounds'); $app->post('/player', 'createPlayer'); $app->post('/round', 'createRound');

// Run Slim $app->run();

function createRound() {

 // Buch of stuff in here

}

I've tried using GLOBALS['appID'] and global $appID; Also tried createRound($appID);

If you have any thoughts or could point me in the right direction that would be awesome! Thanks for your help!
Kane

  1. Support Staff 1 Posted by Brian Nesbitt on 23 Jul, 2012 04:43 PM

    Brian Nesbitt's Avatar

    Both of those should work.

    What does it give you if you do a var_dump($GLOBALS['appID']); just before $app->run(); and then also if you add one as the first line of the createRound() method.

    You can also store the appID variable in the slim environment.

  2. 2 Posted by Kane Cochran on 23 Jul, 2012 05:04 PM

    Kane Cochran's Avatar

    Thanks for the super quick response, Brian. I've narrowed it down a slight bit more. The variables are actually getting pulled in so its not directly a variable issue with the globals as I thought.

    Part of the function calls a class that I include at the top of the services php. When I call the class outside of Slim (before I even run it) everything works as expected. If I try to call the class from my own basic function before Slim, it works fine. However, when I try to call it from within the function associated with a Route, it doesn't work and the class thinks the vars are empty.

    Is there anything special that I need to do to ensure any included class has access? This probably shows my ignorance here, obviously theres a ton more going on when Slim calls my function than just a simple one I create.

    Thanks again!

  3. Support Staff 3 Posted by Brian Nesbitt on 23 Jul, 2012 05:11 PM

    Brian Nesbitt's Avatar

    Is it an instance method... are you sure the object instance is setup correctly?
    Is it a static method of the class?
    Can you post some code or snippets from your code?

    I assume you are using an earlier version than PHP 5.3 since you are not using anonymous functions?!? If you have PHP 5.3+ you could just be doing function createRound() use ($appID,$instance) { } rather than having to access globals.

  4. 4 Posted by Kane Cochran on 23 Jul, 2012 05:23 PM

    Kane Cochran's Avatar

    Hey Brian -

    Yes, I've coded for <5.3. We just moved the server to 5.3. over the weekend and I haven't had a chance to update the code to use anonymous functions, but definitely will.

    I'm using the Urban Airship php library for push notifications - https://github.com/urbanairship/php-library/blob/master/urbanairshi...

    I initialize it above the code setting up Slim:

    require_once urbanairship.php;
    $airship = new Airship($APP_KEY, $APP_MASTER_SECRET);
    

    To call the library I'm using this inside createRound()

    $message = array(aps=>array(alert=>Testing a push notification update ));
    $airship->push($message, array($TEST_DEVICE_TOKEN), null, null);`
    

    This works just fine from my own function or if I call it directly from outside Slim. But once inside that function I get this error: Non-static method PEAR::getStaticProperty() should not be called statically, assuming $this from incompatible context

    Which is actually called from HTTP/Request.php which the Urban Airship library uses to push messages to their server.

    I appreciate your help, Brian!

  5. Support Staff 5 Posted by Brian Nesbitt on 24 Jul, 2012 02:05 AM

    Brian Nesbitt's Avatar

    Looks like its an issue with E_STRICT error_reporting. Its somewhat documented here:
    http://pear.php.net/bugs/bug.php?id=12616

    Slim uses error_reporting(E_ALL | E_STRICT); which gets set when you require it. I think this accounts for it running fine in your testing and it not working inside the Slim route. It would also not work after Slim.php is required and the error_reporting is set.

    That HTTP library was last released in 2008. Maybe its time to fork the urbanairship library :-)

  6. 6 Posted by Kane Cochran on 24 Jul, 2012 02:22 AM

    Kane Cochran's Avatar

    Hi Brian - I was afraid of that, but honestly, given the popularity of Urban Airship, I just kept thinking there had to be something else I was missing and that it couldn't be them using a deprecated package!

    I'm going to get the new HTTP_Request2 on the server, modify the library and give that a go.

    I really appreciate your quick attention to this question. You guys have done an awesome job with Slim and I'm thankful for all the work you've put into this.

    Kane

  7. Support Staff 7 Posted by Brian Nesbitt on 24 Jul, 2012 02:23 AM

    Brian Nesbitt's Avatar

    Good luck!

  8. Brian Nesbitt closed this discussion on 24 Jul, 2012 02:23 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac