php - Accessing variables from within a function within a class (scopes?) -


i'm new classes, , i've been looking online kind of tutorial on this, unfortunately i've been unsuccessful @ finding solution. guys give me appreciated.

i have 2 files.

1) variables.inc.php:

$myvar = "hello world";

2) myclass.php:

include("variables.inc.php"); class myclass {      function dosomething() {         echo "myvar: $myvar";     } }

the problem: $myvar returns empty. tried adding line between function dosomething() { , echo...: global $myvar; doesn't seem work way either. suggestions?

$myvar defined in global scope.

if needs accessed then

function dosomething() { global $myvar; echo "myvar: $myvar"; } 

however usage of global variables in other scopes considered bad practice.

see variable scope chapter in official php manual more detailed explanation.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -