global variable in class php -
i want use variable global in recursive function defined in class. want this.
<?php class copycontroller extends basecontroller { function copycontroller () { $foo="123" ; function recursive () { global $foo ; echo $foo ; recursive() ; } recursive(); } }
in origin code have condition stop recursive.output null
. can me ?
you can't nest functions that. can this:
class copycontroller extends basecontroller { function copycontroller() { $foo="123"; $this->recursive(); } function recursive() { global $foo; echo $foo; $this->recursive() } }
also notice using global variables considered bad practice. i'm not sure goal is, may better define class property $foo
, , access instead:
class copycontroller extends basecontroller { protected $foo; function copycontroller() { $this->foo = "123"; $this->recursive(); } function recursive() { echo $this->foo; $this->recursive() } }
Comments
Post a Comment