Avatar of stargateatlantis
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

/*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 */

Open in new window

PHP

Avatar of undefined
Last Comment
Mark Brady

8/22/2022 - Mon
Ray Paseur

Suggest you extend the db class and add the retVal function as a public method.  Then you can write something like $db->retval(...);
Mark Brady

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
stargateatlantis

ASKER
This is the class I am using

http://code.google.com/p/php-pdo-wrapper-class/


Can somebody show me a example with the retval function just something simple
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
SOLUTION
Mark Brady

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
stargateatlantis

ASKER
Why can't you do something like this

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 */


    }

?>

Open in new window

SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Mark Brady

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

$this-> is the most forgotten detail of object oriented coding.  We all slip up from time to time!
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Mark Brady

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

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
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.