Reputation: 1707
I want to use a variable as global in a recursive function defined in a class. I want something like this.
<?php
class copycontroller extends BaseController {
function copycontroller () {
$foo="123" ;
function recursive () {
global $foo ;
echo $foo ;
recursive() ;
}
recursive();
}
}
in my origin code I have a condition for stop recursive.output is NULL
. Can anyone help me ?
Upvotes: 0
Views: 74
Reputation: 1824
You can't nest functions like that. You can do something like this:
class copycontroller extends BaseController {
function copycontroller() {
$foo="123";
$this->recursive();
}
function recursive() {
global $foo;
echo $foo;
$this->recursive()
}
}
Also notice that using global variables is considered bad practice. I'm not sure what your goal is, but it may be better to define class property $foo
, and access it instead:
class copycontroller extends BaseController {
protected $foo;
function copycontroller() {
$this->foo = "123";
$this->recursive();
}
function recursive() {
echo $this->foo;
$this->recursive()
}
}
Upvotes: 2