yanviau Posted July 14, 2010 Share Posted July 14, 2010 Hola to all php coder, Probably an easy one for you. Page.php include ('class/general.php'); $instance = new General(); function something() { $instance->check() } general.php <?php class General { function check() { echo "world"; } } Why the call of my class function doesnt work ? when my class declaration is outside the function = BOUM error When my class declaration is inside my function = working So i understand the fact than i need a global variable or something like that but i try to include ('class/general.php'); global $instance; $instance = new General(); And it's not the solution. Someone have any tips ? Sorry for my english. Quote Link to comment Share on other sites More sharing options...
Mchl Posted July 14, 2010 Share Posted July 14, 2010 You need to familiarize yourself with the concept of variable scope http://php.net/manual/en/language.variables.scope.php In your example include ('class/general.php'); $instance = new General(); function something($object) { $object->check() } something($instance); Quote Link to comment Share on other sites More sharing options...
yanviau Posted July 14, 2010 Author Share Posted July 14, 2010 Thanks a lot for your help ! im gonna read this any other solution ? i would like prefer to have a variable that i don't need to pass in the function if it's possible. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted July 14, 2010 Share Posted July 14, 2010 i would like prefer to have a variable that i don't need to pass in the function if it's possible. Why? That's how functions are supposed to be written. You can use a global statement, but then you are limiting the usefulness of the function. Ken Quote Link to comment Share on other sites More sharing options...
yanviau Posted July 14, 2010 Author Share Posted July 14, 2010 Sorry maybe some feedback from other language, im new with php in my mind the easiest and normal thing is something like this but i know it's doesn't work. include ('class/general.php'); global $instance = new General(); function something() { $instance->check() } Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted July 14, 2010 Share Posted July 14, 2010 The global keyword in PHP is used inside the function. Ken Quote Link to comment Share on other sites More sharing options...
Mchl Posted July 14, 2010 Share Posted July 14, 2010 Yeah, different languages have different approach towards variable scope. In PHP scopes are isolated. Variables from outside are not visible inside a function. In JavaScript they are, but can be overriden by local variables. Quote Link to comment Share on other sites More sharing options...
yanviau Posted July 14, 2010 Author Share Posted July 14, 2010 thx for your help Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.