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