Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Help with some PHP

Name: OP !0vfAtk0uVQ 2012-08-31 13:33

ok /prog/ please help me out if any of you can. I NEED help, so please if you cannot or do not want to help at least keep trolling to a minimum.
So, I have this PHP object.
$connection = mysqli::__construct
(i.e. the object that tells the program I have made a connection to the database)
Now, I want to pass $con (which is an object) as an argument to a function, and then inside that function I want to call a method to the object $con, something like this:

function result($con) {
  $res = $con->query();
}
result($connection);

In this case the method is the query() method.
Well, the thing is I cannot do this, because, apparently, the $con that lives inside the function is NULL for some reason.
Even though since PHP 5 all objects that pass as arguments to functions pass by reference (or more precisely, by reference to their identifier), yet I tried to explicitly call it by reference, by putting a & in front of the argument, like this

function result(&$con) {
  $res = $con->query();
}
result($connection);

to no avail. $con is still NULL, if I do a var_dump() to it.
If I var_dump() the $connection, outside of the function, it will tell me that it's an object[etc], so I know that it initialises correctly. So I do something wrong when I pass it as an argument?..
On way to solve the problem, is to do the following

function() {
     $con = mysqli::__construct
     $res = $con->query();
}

but I do not want to create connections every time I  call the function, I want one "central" connection.
Any help will be much appreciated.

Name: Anonymous 2012-08-31 14:43

As i can't see the whole code, i'm just assuming.

If $con is a global variable, you must use the "global" keyword to use it inside another function.

Yes, PHP is shit.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List