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. Link to comment https://forums.phpfreaks.com/topic/207721-class-call-in-a-function/ 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); Link to comment https://forums.phpfreaks.com/topic/207721-class-call-in-a-function/#findComment-1085897 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. Link to comment https://forums.phpfreaks.com/topic/207721-class-call-in-a-function/#findComment-1085910 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 Link to comment https://forums.phpfreaks.com/topic/207721-class-call-in-a-function/#findComment-1085932 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() } Link to comment https://forums.phpfreaks.com/topic/207721-class-call-in-a-function/#findComment-1085935 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 Link to comment https://forums.phpfreaks.com/topic/207721-class-call-in-a-function/#findComment-1085941 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. Link to comment https://forums.phpfreaks.com/topic/207721-class-call-in-a-function/#findComment-1085951 Share on other sites More sharing options...
yanviau Posted July 14, 2010 Author Share Posted July 14, 2010 thx for your help Link to comment https://forums.phpfreaks.com/topic/207721-class-call-in-a-function/#findComment-1085954 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.