We help IT Professionals succeed at work.

Variable scope in php

stargateatlantis
on
256 Views
Last Modified: 2012-08-13
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

Comment
Watch Question

Most Valuable Expert 2011
Author of the Year 2014

Commented:
Suggest you extend the db class and add the retVal function as a public method.  Then you can write something like $db->retval(...);
Mark BradyPrincipal Data Engineer
CERTIFIED EXPERT

Commented:
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

Author

Commented:
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
Mark BradyPrincipal Data Engineer
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
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

Mark BradyPrincipal Data Engineer
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Mark BradyPrincipal Data Engineer
CERTIFIED EXPERT

Commented:
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.
Most Valuable Expert 2011
Author of the Year 2014

Commented:
$this-> is the most forgotten detail of object oriented coding.  We all slip up from time to time!
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Mark BradyPrincipal Data Engineer
CERTIFIED EXPERT

Commented:
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...
CERTIFIED EXPERT

Commented:
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
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Principal Data Engineer
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.