calling a method from inside a function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
olly
Forum Newbie
Posts: 3
Joined: Fri Nov 11, 2005 4:05 am
Location: Cornwall, UK

calling a method from inside a function

Post by olly »

Apologies in advance if this is a daft question.
I am trying to put all my database handling routines into a class (mysqldatabase). So far so good. I want to call one of the methods from this call one of the methods in this class from inside the function "selectTable()", but it responds with:

Fatal error: Call to a member function on a non-object in /var/www/html/database/listviews/listviews.php on line 60

(line 60 being the line indicated in the code)

So I tried the exact same code, but not in a function (the commented out block below) and it worked fine (it did have an issue with my close method mind you).

So my question is this: can methods be called from inside functions like this, or is there some scoping issue that I am missing?

Code: Select all

<?php
Require 'class_database.php';
  
function selectTable(){
  // get table list from db and build select element from result
  $HTMLselect = "<select>";

  // *** the line won't work here ***
  $tablelist = $MyDatabase->runquery("SHOW TABLES", "NUM");

  foreach($tablelist as $key=>$value)
    $HTMLselect .= "<option>$value[0]</option>";
  $HTMLselect .= "</select>";
  return $HTMLselect;
  }

$MyDatabase = new mysqldatabase('localhost', 'mydatabase');
echo selectTable();
/*
  // get table list from db and build select element from result
  $HTMLselect = "<select>";

  // *** the line works here ***
  $tablelist = $MyDatabase->runquery("SHOW TABLES", "NUM");

  foreach($tablelist as $key=>$value)
    $HTMLselect .= "<option>$value[0]</option>";
  $HTMLselect .= "</select>";
*/

$MyDatabase->close();
?>
Here is my (rather primitive at the moment) class

Code: Select all

<?php
class mysqldatabase {
  var $host;
  var $name;
  var $link;

  function mysqldatabase($hostname, $databasename){
    $this->host = $hostname;
    $this->name = $databasename;
    // select database
    $this->link = mysql_connect($this->host) 
      or die("Could not connect to host '$this->host': " . mysql_error());
    mysql_select_db($this->name) 
      or die("Could not select database '$this->name':" . mysql_error());
    }

  function close(){
    // close database
    mysql_close($this->link);
    }

  function runquery($querystring, $returntype){
    // run a query and return a 2d array of the results
    $returntype = strtoupper($returntype);
    if (($returntype !== 'ASSOC') and ($returntype !== 'NUM')) 
      $returntype = 'BOTH';
    $queryresult = mysql_query($querystring)
      or die('Query failed: ' . mysql_error());
    $returnarray = array();
    $linecount = 0;
    while($line = mysql_fetch_array($queryresult, constant("MYSQL_$returntype")))
      $returnarray[$linecount++] = $line;
    return $returnarray;
    }
  }
?>
Any light shed on this would be much appreciated.
Thanks in advance, Olly.
Last edited by olly on Fri Nov 11, 2005 7:19 am, edited 1 time in total.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Hi, please use PHP tags when posting PHP code (i.e. edit your post and swap the code tags for php tags :))

You'll need to decalre the global scope of the object before using it's properties and/or methods :)

Code: Select all

<?php

function someFunction () 
{
    global $obj;
    $obj->method();
}

?>
:)
olly
Forum Newbie
Posts: 3
Joined: Fri Nov 11, 2005 4:05 am
Location: Cornwall, UK

Post by olly »

Fixed tags.

I kinda assumed that variables, objects etc. declared in the main part of the script (ie. outside of any functions) were automatically global, but on reading up I find I was incorrect. D'oh!

Thanks very much Jenk! :)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

They are, it's the other end of the stick where the problem lies - the scope of the function :)
olly
Forum Newbie
Posts: 3
Joined: Fri Nov 11, 2005 4:05 am
Location: Cornwall, UK

Post by olly »

Oh right...so the global object gets overridden by a ficticious local one, thus stopping it being accessed? :?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Yup, unless you declare it global :)

Code: Select all

<?php

function myFunc () 
{
    echo $a; //outputs NULL and issues error of E_NOTICE level (undefined variable)
}

$a = '1';

myFunc();

?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Post Reply