blmg2009 Posted October 24, 2014 Share Posted October 24, 2014 Hi, I'm struggling to understand how to load a PHP classes for my database into a php function. My class in on another file, which is included into my page. I then have a function that usings the class of $database->query in the function, but for some reason this doesn't work. However the class works on the page outside of the function, is there someway I must load the class into my function? Thanks for reading. Quote Link to comment https://forums.phpfreaks.com/topic/292028-how-to-load-a-php-class-into-a-php-functions/ Share on other sites More sharing options...
blmg2009 Posted October 24, 2014 Author Share Posted October 24, 2014 include_once"database.php"; function test($one, $two, $three) { $database->query(...); /// doesn't work inside this function } $database->query(...); /// works outside the function Quote Link to comment https://forums.phpfreaks.com/topic/292028-how-to-load-a-php-class-into-a-php-functions/#findComment-1494580 Share on other sites More sharing options...
mac_gyver Posted October 24, 2014 Share Posted October 24, 2014 the answer is similar to one of your previous threads, you pass the instance of the database into your function as a call time parameter. 1 Quote Link to comment https://forums.phpfreaks.com/topic/292028-how-to-load-a-php-class-into-a-php-functions/#findComment-1494581 Share on other sites More sharing options...
blmg2009 Posted October 24, 2014 Author Share Posted October 24, 2014 Thank you for taking the time to reply, I have tried this too but I get the following error: Argument 4 passed to test() must be an instance of Database, none given Sorry I'm new to PHP. Quote Link to comment https://forums.phpfreaks.com/topic/292028-how-to-load-a-php-class-into-a-php-functions/#findComment-1494582 Share on other sites More sharing options...
blmg2009 Posted October 24, 2014 Author Share Posted October 24, 2014 include_once"database.php"; function test($one, $two, $three, Database $database) { $database->query(...); /// doesn't work inside this function } $database->query(...); /// works outside the function Quote Link to comment https://forums.phpfreaks.com/topic/292028-how-to-load-a-php-class-into-a-php-functions/#findComment-1494584 Share on other sites More sharing options...
ginerjm Posted October 24, 2014 Share Posted October 24, 2014 WHAT is that word 'Database' doing in that header? You need to pass the variable that represents your current instance of database class. So test(!one,$two,$three,$database) is what you want. IMHO - if you are such a newcomer to PHP, why are you getting into classes and whatever else (objects?) when you don't even know how to pass arguments to a function or how to format the header of one? That is a basic premise of functions in any language that supports functions and you don't understand it. Quote Link to comment https://forums.phpfreaks.com/topic/292028-how-to-load-a-php-class-into-a-php-functions/#findComment-1494585 Share on other sites More sharing options...
requinix Posted October 24, 2014 Share Posted October 24, 2014 WHAT is that word 'Database' doing in that header?Type hinting. blmg2009, keep it there. It's a good thing. 1 Quote Link to comment https://forums.phpfreaks.com/topic/292028-how-to-load-a-php-class-into-a-php-functions/#findComment-1494621 Share on other sites More sharing options...
ginerjm Posted October 24, 2014 Share Posted October 24, 2014 Type hinting in PHP? I have not seen that mentioned before. My bad. Quote Link to comment https://forums.phpfreaks.com/topic/292028-how-to-load-a-php-class-into-a-php-functions/#findComment-1494624 Share on other sites More sharing options...
KevinM1 Posted October 24, 2014 Share Posted October 24, 2014 Type hinting in PHP? I have not seen that mentioned before. My bad. Type hinting has been in PHP for, well, years. Quote Link to comment https://forums.phpfreaks.com/topic/292028-how-to-load-a-php-class-into-a-php-functions/#findComment-1494639 Share on other sites More sharing options...
ginerjm Posted October 24, 2014 Share Posted October 24, 2014 I just have not run across it. That's all. Quote Link to comment https://forums.phpfreaks.com/topic/292028-how-to-load-a-php-class-into-a-php-functions/#findComment-1494640 Share on other sites More sharing options...
trq Posted October 25, 2014 Share Posted October 25, 2014 I just have not run across it. That's all. Then you're doing it wrong. Quote Link to comment https://forums.phpfreaks.com/topic/292028-how-to-load-a-php-class-into-a-php-functions/#findComment-1494650 Share on other sites More sharing options...
ginerjm Posted October 25, 2014 Share Posted October 25, 2014 doing what wrong? Quote Link to comment https://forums.phpfreaks.com/topic/292028-how-to-load-a-php-class-into-a-php-functions/#findComment-1494654 Share on other sites More sharing options...
trq Posted October 25, 2014 Share Posted October 25, 2014 doing what wrong? PHP 1 Quote Link to comment https://forums.phpfreaks.com/topic/292028-how-to-load-a-php-class-into-a-php-functions/#findComment-1494673 Share on other sites More sharing options...
.josh Posted October 25, 2014 Share Posted October 25, 2014 include_once"database.php"; function test($one, $two, $three, Database $database) { $database->query(...); /// doesn't work inside this function } $database->query(...); /// works outside the function So, some debugging 101 here.. you got an error that was essentially the result the variable not being within the scope of the function. So you updated your function declaration and now you are getting another error, which basically means you aren't passing to test what it expects. To me, this sounds like you likely essentially have the same issue before, except backing up the chain a notch. So, where/how are you calling test()? Did you update where you are calling it to have it actually pass a Database type object when you call it? If so, are you calling test() within some other function, and $database isn't exposed to it, in the same way it wasn't exposed here? Follow the trail, make sure $database is actually exposed, same as you already did here. Quote Link to comment https://forums.phpfreaks.com/topic/292028-how-to-load-a-php-class-into-a-php-functions/#findComment-1494708 Share on other sites More sharing options...
blacknight Posted October 25, 2014 Share Posted October 25, 2014 if $database is defined in the db file you could just call it as a global in the function like function test($one, $two, $three) { global $database; $database->query(...); /// doesn't work inside this function } some people dont like to do this but it does work Quote Link to comment https://forums.phpfreaks.com/topic/292028-how-to-load-a-php-class-into-a-php-functions/#findComment-1494725 Share on other sites More sharing options...
mac_gyver Posted October 25, 2014 Share Posted October 25, 2014 @blacknight, it's not that people don't like it, it's that it produces bad code that's harder to maintain because it requires that the programmer keep track of a greater amount of information, making his job harder and making it harder for anyone in the future to make use of the code or make changes to the code. using global to being a value into a function breaks the black-box model of writing functions. the only interaction a function should have with the calling code is at the point where the function gets called. 1 Quote Link to comment https://forums.phpfreaks.com/topic/292028-how-to-load-a-php-class-into-a-php-functions/#findComment-1494729 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.