stargateatlantis
asked on
Variable scope in php
I am trying to access the $db variable in my custom function but it isn't working not sure why. It has something to do with scope but I would like the function to access the $db variable.
Here is my code below
Here is my code below
/*Connect to the database.*/
$db=new db("mysql:host=localhost;dbname=mydb", "mydb", "mydb123");
/*Get database value*/
function retVal($table,$field,$val,$returnVal)
{
$dataReturn = $db->select($table, $field."= '".$val."'");
/*Loop THru array and load it with values */
foreach ($dataReturn[0] as $key=>$value) {
$dataArray[$key]=$value;
}
return $dataArray[$returnVal];
}
/*End function return value */
Suggest you extend the db class and add the retVal function as a public method. Then you can write something like $db->retval(...);
When you are inside a class method like that, you must access the properties by using $this->property and not just the property name. So try using $this->db in place of $db
ASKER
This is the class I am using
http://code.google.com/p/p hp-pdo-wra pper-class /
Can somebody show me a example with the retval function just something simple
http://code.google.com/p/p
Can somebody show me a example with the retval function just something simple
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Why can't you do something like this
But now i was trying to do something like this $db->retval();
But now i was trying to do something like this $db->retval();
<?php
class dbfunc extends db
{
/*Get database value*/
public function retVal()
{
return "hello";
}
/*End function return value */
}
?>
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Why can't you do something like this
But now i was trying to do something like this $db->retval();
Because inside your new extended class in order for you to call your function retVal() to return 'hello' or whatever, you need to call it like this
$this->retVal().
If you wanted to call the databases function you would not bother to overwrite its function by declaring a new one with the same name in your new class. Inside your new extended class you can call the retVal() function in the parent class by parent::retVal() or $this->retVal() but if you overwrite the function in your extended class, you can only call the origianl by using parent::retVal(). If you call $this->retVal() you are calling your new function.
But now i was trying to do something like this $db->retval();
Because inside your new extended class in order for you to call your function retVal() to return 'hello' or whatever, you need to call it like this
$this->retVal().
If you wanted to call the databases function you would not bother to overwrite its function by declaring a new one with the same name in your new class. Inside your new extended class you can call the retVal() function in the parent class by parent::retVal() or $this->retVal() but if you overwrite the function in your extended class, you can only call the origianl by using parent::retVal(). If you call $this->retVal() you are calling your new function.
$this-> is the most forgotten detail of object oriented coding. We all slip up from time to time!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Yeah Ray only a couple of days ago I spent two hours debugging a class I thought was a logic mistake and like you say, it was a missing $this-> in the end. Bugger...
it does not look to me like the author is doing object programming.
he seems to be just using the object-oriented version of the db api
this-> would probably relevant if the database connection was in a class constructor
this thread is probably a very good example as to why mixing object oriented and procedural code is usually not a great idea (though workable)
if you'd rather do procedural (so am i), use the procedural versions of the APIs. the PHP libraries almost always come with both versions.
if you really need to use an object oriented API such as net/dns from pecl, i suggest you always wrap that API into a procedural one so your main code trunk stays procedural
he seems to be just using the object-oriented version of the db api
this-> would probably relevant if the database connection was in a class constructor
this thread is probably a very good example as to why mixing object oriented and procedural code is usually not a great idea (though workable)
if you'd rather do procedural (so am i), use the procedural versions of the APIs. the PHP libraries almost always come with both versions.
if you really need to use an object oriented API such as net/dns from pecl, i suggest you always wrap that API into a procedural one so your main code trunk stays procedural
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.