Skip to content

Instantly share code, notes, and snippets.

@JeffreyWay
Created October 6, 2011 15:20
Show Gist options
  • Save JeffreyWay/1267664 to your computer and use it in GitHub Desktop.
Save JeffreyWay/1267664 to your computer and use it in GitHub Desktop.
<?php
$someVar = 'some value';
add_action('admin_init', function() {
# I want access to $someVar in here, and don't want it to be a global.
# Is there a way to pass $someVar into the callback as a local variable?
});
@cogsprocket
Copy link

$someVar = 'some value';
addaction('admin_init', function($var = $someVar) {

});

$var should default to $someVar

@RamyTalal
Copy link

Something like this?:

$someVar = 'some value';
addaction('admin_init', function(&$someVar) {

});

@rcky
Copy link

rcky commented Oct 6, 2011

$someVar = 'some value';
add_action('admin_init', function() use($someVar) {
$something = $someVar;
});

@JeffreyWay
Copy link
Author

Okay - use did it. Thanks @rcky! The other two options didn't work for me.

$someVar = 'some value';
add_action('admin_init', function() use($someVar) {
    echo $someVar;
});

@jkudish
Copy link

jkudish commented Oct 6, 2011

Just a note here. This syntax will only work in php 5.3+

@JeffreyWay
Copy link
Author

Yep.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment