Jump to content

Recommended Posts

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...

Link to comment
https://forums.phpfreaks.com/topic/136421-calling-functions-from-other-classes/
Share on other sites

<?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.

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;
    }
}
?>

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 =)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.