0

I'm displaying a relatively "heavy" PHP page. I have several functions that each use a connection to the DB to grab some rows. I've noticed that making a new $con in each function slows things down a lot, so naturally I would like to use the same $con for each function on the page. I tried this:

$con = mysqli_connect("localhost","root","root", "mydb");

function fA($a, $b, &$con = $con)
{}

function fB($a, &$con = $con)
{}

You can see I'm trying to pass a pointer to $con for each function, but I get a syntax error, unexpected $con for each one. (I could pass $con to the functions whenever I call them but doing it this way I don't have to go back and change every function call, and it looks neater)

Juicy
  • 11,840
  • 35
  • 123
  • 212
  • 2
    PHP doesn't work like that. `$con` isn't defined at the time you define the function. Just define as `function fA($a, $b, $con)` and the object `$con` will always be passed by reference. – Michael Berkowski Nov 09 '13 at 20:36
  • In other words, you can't get out of passing a parameter at runtime by "pre-defining" a variable in the function prototype. You can define a default with a constant value, but not with a dynamic (variable) value. – Michael Berkowski Nov 09 '13 at 20:37
  • ok so if I call fA(10, 12) he will automatically get $con? – Juicy Nov 09 '13 at 20:37
  • If you're looking for type hinting, define the argument as `mysqli $con` – nice ass Nov 09 '13 at 20:38
  • 1
    @Juicy No. You need to pass $con as a param, and you should because it makes your code readable. – Michael Berkowski Nov 09 '13 at 20:38
  • @MichaelBerkowski, thanks, I get it. Just to clarify things, what would be the difference between function fA($a,$b,$con) and function fA($a,$b,&$con). Is it better for performance to pass the pointer or not? – Juicy Nov 09 '13 at 20:42
  • 1
    Defining the reference `&$con` is unnecessary and redundant. PHP always passes objects by reference, so the `$con` passed into the function will be the exact same object defined outside the function. – Michael Berkowski Nov 09 '13 at 20:45

2 Answers2

1

You can use:

global $con;

At the beginning of each function's body like so:

function fA($a, $b) {
    global $con
    // ...etc...
}

But this is generally viewed as a bad practice.

A better approach would be to use dependency injection so that your logic would not be dependent on the kind of storage device given to it. Something along these lines:

class LogicExecutor {
    private $con;

    public __construct($con) {
        $this->con = $con;
    }

    public function doTaskOne($a, $b) {
        // you can use $this->con in this scope
    }
}

$con = mysqli_connect('localhost', 'root', 'root', 'mydb');
$le = new LogicExecutor($con);

$le->doTaskOne('a','b');

But even that would couple your code to mysqli very tightly. Have you considered an ORM?

Community
  • 1
  • 1
Bailey Parker
  • 15,599
  • 5
  • 53
  • 91
0

Using global $con is ok, best solution is put the stuff into a class.

If you sticked to your original concept, you should pass $con on every function call.

$con = mysqli_connect("localhost","root","root","mydb");
fA("abraca","dabra",$con);
fB("hocus-pocus",$con);

function fA($a, $b, &$con)
{}

function fB($a, &$con)
{}

Maybe it's better to use $con as the first parameter.

ern0
  • 3,074
  • 25
  • 40