Goafer Posted December 10, 2008 Share Posted December 10, 2008 OK so there seems to be a lot of people on the internet asking this question and it never seems to have a definite answer so here goes: I have a page which calls on 2 class', both of which are initiated: <?php require_once('../includes/db_connect.php'); require_once('../includes/pageFunctions.php'); $connect = new dbconnect(); $func = new pageFunctions(); ?> This allows functions from either class to be executed from the page. What I want is: for one of the functions in the pageFunctions class, to use a function from within the db_connect class. So far the only way i've found of doing it is to re-initiate the db_connect class within the function thus: class pageFunctions { function randomFunction() { $connect = new dbconnect(); This then allows me to use functions from dbconnect() in randomFunction() for example: $connect->query('code'); The question: Is it possible for this to work without re-initiating dbconnect() within randomFunction()? Seeing as they have both been initiated on the active page, why can they not use each other without re-initiating each time? Can they be made global? If so how? I hope this is clear enough? Any help appreciated... Quote Link to comment https://forums.phpfreaks.com/topic/136421-calling-functions-from-other-classes/ Share on other sites More sharing options...
Mark Baker Posted December 10, 2008 Share Posted December 10, 2008 <?php require_once('../includes/db_connect.php'); require_once('../includes/pageFunctions.php'); $connect = new dbconnect(); $func = new pageFunctions($connect); ?> Use the pageFunctions class constructor to store $connect as an attribute within each instance of pageFunctions that you create. Then you can use the dbconnect methods from within pageFunctions. Quote Link to comment https://forums.phpfreaks.com/topic/136421-calling-functions-from-other-classes/#findComment-711937 Share on other sites More sharing options...
flyhoney Posted December 10, 2008 Share Posted December 10, 2008 You need to look into static classes/functions. Make the function you are trying to use static, and then you can call it like so: <?php $result = dbconnect::someFunction(); ?> So in the dbconnect class it would look like this: <?php class dbconnect { public static function someFunction() { return true; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/136421-calling-functions-from-other-classes/#findComment-711939 Share on other sites More sharing options...
DarkWater Posted December 10, 2008 Share Posted December 10, 2008 @flyhoney: No, you wouldn't really want static functions for this. Quote Link to comment https://forums.phpfreaks.com/topic/136421-calling-functions-from-other-classes/#findComment-711949 Share on other sites More sharing options...
premiso Posted December 10, 2008 Share Posted December 10, 2008 You could just make a functions include, where you create functions the same as what you want to call in the DB. IE: <?php require('db.class.php'); $db = new dbClass(); function query($query) { global $db; $db->query($query); } ?> That way you can have the valid SQL connection and use the db class functions anywhere in your script, where it is in class a or class b. To call it you would just do query($sql); I found that to be the quickest way so you do not have to have the DB class instantiated about 10 times or passed by reference to each class 10 different times =) Quote Link to comment https://forums.phpfreaks.com/topic/136421-calling-functions-from-other-classes/#findComment-711950 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.