jackhammer013
jackhammer013

Reputation: 2295

Access Class Method / Function PHP

I have an index.php and a Regions.php which is a class. I'm trying to call a method from Regions class to my index.php but it seems I can't access it.It says:

Call to undefined function getRegionInfo() in C:\xampp\htdocs\exercise4\index.php on line 7

But I already included the files I need. Here's the code.

index.php

$regions = new Regions;
$regions = getRegionInfo();
$regions = get_regions();

Here's my Region class

class Regions
{

 public function getRegionInfo()
 {
    $this->get_regions();
 }

 private function get_regions()
 {
    global $db;
    $result = array();
    $sql  = "SELECT RegionID, RegionName, IslandID FROM ref_regions";
    $stmt = $db->prepare($sql);
    $stmt->execute();
    $stmt->setFetchMode(PDO::FETCH_ASSOC);
    while ($row = $stmt->fetch()) 
    {
      array_push($result,array($row['RegionID'], $row['RegionName'], $row['IslandID']));
    }     
    return $result;
 }

}

Upvotes: 0

Views: 90

Answers (3)

Cully
Cully

Reputation: 6975

First, the correct index.php would look something like this:

$regionObj = new Regions();
$regions = $regionObj->getRegionInfo();

You need to indicate that you're calling the getRegionInfo function on a specific instance of the Regions object ($regionsObj in the example above).

Second, there is no point in creating a public and a private method in this case. Including the extra private method is unnecessary because all the public method does is call the private method. Just put all the code in the public method. You could just have:

class Regions
{

 public function getRegionInfo()
 {
    global $db;
    $result = array();
    $sql  = "SELECT RegionID, RegionName, IslandID FROM ref_regions";
    $stmt = $db->prepare($sql);
    $stmt->execute();
    $stmt->setFetchMode(PDO::FETCH_ASSOC);
    while ($row = $stmt->fetch()) 
    {
      array_push($result,array($row['RegionID'], $row['RegionName'], $row['IslandID']));
    }     
    return $result;
 }

}

Third, you shouldn't have your $db be a global variable. Instead, send it as a parameter to the constructor of your Regions class:

class Regions
{

 private $database;

 public function __construct(PDO $database) {
     $this->database = $database;
 }

 public function getRegionInfo()
 {
    $result = array();
    $sql  = "SELECT RegionID, RegionName, IslandID FROM ref_regions";
    $stmt = $this->database->prepare($sql);
    $stmt->execute();
    $stmt->setFetchMode(PDO::FETCH_ASSOC);
    while ($row = $stmt->fetch()) 
    {
      array_push($result,array($row['RegionID'], $row['RegionName'], $row['IslandID']));
    }     
    return $result;
 }

}

Then create the Regions object like this:

$regionsObj = new Regions($db);

Fourth, you don't need to populate the results array yourself. Instead, you can just return the results of the fetchAll function on your $stmt object:

 public function getRegionInfo()
 {
    $sql  = "SELECT RegionID, RegionName, IslandID FROM ref_regions";
    $stmt = $this->database->prepare($sql);
    $stmt->execute();
    $stmt->setFetchMode(PDO::FETCH_ASSOC);
    return $stmt->fetchAll();
 }

Your final class, with all the changes, will look like this:

class Regions
{

 private $database;

 public function __construct(PDO $database) {
     $this->database = $database;
 }

 public function getRegionInfo()
 {
    $sql  = "SELECT RegionID, RegionName, IslandID FROM ref_regions";
    $stmt = $this->database->prepare($sql);
    $stmt->execute();
    $stmt->setFetchMode(PDO::FETCH_ASSOC);
    return $stmt->fetchAll();
 }

}

A final note, I'm not sure if you just didn't include this portion of your code, and this was mentioned in another answer, I'll include it here just to be complete, make sure to include your Regions.php file in index.php before referencing the Regions class:

include_once("Regions.php");

Upvotes: 3

Hannes Schneidermayer
Hannes Schneidermayer

Reputation: 5785

Class Methods are accessed by using the ->symbol. So use:

$regions = new Regions();
$regions->getRegionInfo();
$regions->get_regions();

And change your get_regions() from private to public, if you want to call it externally.

Upvotes: 0

Eddie
Eddie

Reputation: 26854

How about this.. You need to include_once the file before you can use it

index.php

include_once("Regions.php");

$regions = new Regions;
$x = $regions->getRegionInfo(); //this is temp var name

Upvotes: 0

Related Questions