so I have these functions
function a(){
int c = 1;
b(function(){echo $c;});
}
function b($code){
$code();
}
but somehow $c becomes undefined in the anonymous function
I know it's bacause that the anonymous function is it's own scope, but is there someway to make this work?
Yes: you can use "use" statement.
function a()
{
$c = 1;
b(function() use ($c) {
echo $c;
});
}
function b($code){
$code();
}
http://php.net/manual/en/language.variables.scope.php
When you put $c inside a function it's considered to be a local scope variable.
Related
I have this in index.php
require('include\inc\config.inc');
When I created a function which require some variable from config.inc
When I run, it says the variable is missing
I need to redeclare the config.inc else the function will never work.
Is there a way to work around?
Now I have this
function test(){
require('include\inc\config.inc');
echo $tmp;
}
instead of
require('include\inc\config.inc');
function test(){
echo $tmp;
}
You should read about the variable scope. The $tmp variable is global and is not visible inside any function, unless declared with global
require('include\inc\config.inc');
function test(){
global $tmp;
echo $tmp;
}
Your variable is out of scope in the test() function as previously mentioned and using globals is not considered good practice. You need to pass the variable $tmp into the function test() like this:-
function test($tmp)
{
echo $tmp;
}
Then call the function like this:-
require_once 'include/inc/configure.inc'
test($tmp);
I'm not fluent in PHP, but can't you write:
require('include\inc\config.inc');
function test() use ($tmp) {
echo $tmp;
}
it is possible to assign to a class variable a function at runtime to be executed? a kind of "function pointer" like C
something like this: (this won't work because sum is out of the scope of A, but this is the pattern i mean)
class A {
public $function_name;
public functon run($arg1,$arg2){
$function_name($arg1,$arg2);
}
}
function sum($a,$b){
echo $a+$b;
}
$a=new A();
$a->function_name='sum';
$a->run();
[edit]
i know there is "call_user_func" but it need as i understand to have the function in the scope or use a public class method
You could use an anonymous function if you use PHP >5.3.0:
$sum = function($a, $b) {
return $a+$b;
}
$a->function_name = $sum;
Using call_user_func_array:
<?php
class A {
public $function_name;
public function run($arg1,$arg2){
return call_user_func_array( $this->function_name, array($arg1, $arg2 ) );
}
}
function sum($a,$b){
return $a+$b;
}
$a=new A();
$a->function_name= 'sum';
var_dump( $a->run(1,1) ); //2
?>
It works regardless of scope. You just gotta call it using call_user_func. I also fixed a couple of typos in your example.
<?php
class A {
public $function_name;
public function run($arg1, $arg2) {
call_user_func($this->function_name, $arg1, $arg2);
}
}
function sum($a, $b){
echo $a + $b;
}
$a = new A();
$a->function_name = 'sum';
$a->run(2, 3);
?>
Live example
Another way is to make use variable variables (applicable to object method)
public static function sum($arg1, $arg2)
{
..
}
public function run($arg1, $arg2)
{
$func = $this->function_name;
$func( $arg1, $arg2); <-- procedural call
self::$func($arg1, $arg2); <-- static method call
}
Use any variation of the Callback pseudo type.
Use it with call_user_func or call_user_func_array
The manual gives great examples of usage for the above.
Also see the new php 5.4 Closure::bindTO method if you want to be able to easily use the $this keyword in it.
I don't understand what's going on with this. I need to call Func1 from Func2 and parametr for Func1 should be given inside the object.
class MyClass {
function Func1($a) {
return $a;
}
function Func2() {
echo $this->Func1($a);
}
}
$c = new MyClass();
$c->Func1('parametr'); // prints: 1
$c->Func2();
What about setting the parameter as class variable (property)?
class MyClass {
private $a;
function Func1($a) {
$this->a = $a;
return $a;
}
function Func2() {
echo $this->Func1($this->a);
}
}
This sets the parameter first time you call Func1. Then everytime you call Func2, it uses the parameter. You can also skip passing the parameter like this:
class MyClass {
private $a;
function Func1($a = null) {
if ($a === null) {
return $this->a;
} else {
$this->a = $a;
return $a;
}
}
function Func2() {
echo $this->Func1();
}
}
I.e if you call func1 without any parameter, it uses the stored variable (property), otherwise it uses the given parameter. This can be used in various ways depending on your exact needs.
The instruction:
echo $this->Func1($a);
is wrong: the variable $a is out of the scope of Func2. $a is a parameter of Func1 so is only int he scope of Func1.
You should read more about variable scopes at PHP http://php.net/manual/en/language.variables.scope.php
quick glimpse:
1) you can have global variables. to access those, use keyword global in functions that need access to that
2) you can have local variables, available only within a scope of a function
3) you can pass references to variables, so that variable from one scope is made accessible to other function/scope
4) you can have objects's internal variables of different kind (private, public, protected, static)
I suggest you get familiar with this stuff real well.
As for you code, problem is obvious. In Func2 the $a is local variable, thus when passed to $this->Func1($a), it is undefined. As your example code suggests, you might want to introduce class property private $a, and then use that. e.g.:
class X {
private $a;
function set($val){
$this->a = $val;
}
function get(){
return $this->a;
}
function doSomethingWithA(){
$this->set($this->get() * 2);
}
}
I have a php file:
<?php
$a = 1;
function test(){
echo $a;
}
test();
?>
And I get this error:
Notice: Undefined variable: a in X:\...\test.php on line 4
Using XAMPP # 32bit W7.
Variables have function scope. $a inside the function is not the same as $a outside the function. Inside the function you have not defined a variable $a, so it doesn't exist. Pass it into the function:
$a = 1;
function test($a) {
echo $a;
}
test($a);
You have trouble understanding variable scope. $a is defined in the global scope, but not in the function scope. If you want your function to know what $a contains, you have two choices :
Make it global (usually a bad solution)
Add a new argument to your function, and pass your variable to your function
You can use global as advised, but that is bad practice. If you need variables in a function from outside the function then pass them as parameters.
$a = 1;
function test($a) {
echo $a;
}
test($a);
For some reason this function won't return the value ciao:
$a = "ciao";
function a() {
return $a;
}
I have no idea why.
Functions can only return variables they have in their local space, called scope:
$a = "ciao";
function a() {
$a = 'hello`;
return $a;
}
Will return hello, because within a(), $a is a variable of it's own. If you need a variable within the function, pass it as parameter:
$a = "ciao";
function a($a) {
return $a;
}
echo a($a); # "ciao"
BTW, if you enable NOTICES to be reported (error_reporting(-1);), PHP would have given you notice that return $a in your original code was using a undefined variable.
In PHP, functions don't have access to global variables. Use global $a in body of the function or pass the value of $a as parameter.
$a is not in scope within the function.
PHP does not work with a closure like block scope that JS works with for instance, if you wish to access an external variable in a function, you must pass it in which is sensible, or use global to make it available, which is frowned on.
$a = "ciao";
function a() {
global $a;
return $a;
}
or with a closure style in PHP5.3+
function a() use ($a) {
return $a;
}