简体   繁体   English

访问类方法/函数PHP

[英]Access Class Method / Function PHP

I have an index.php and a Regions.php which is a class. 我有一个index.phpRegions.php这是一个类。 I'm trying to call a method from Regions class to my index.php but it seems I can't access it.It says: 我正在尝试从Regions类调用方法到index.php,但似乎无法访问它,它说:

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;
 }

}

First, the correct index.php would look something like this: 首先,正确的index.php如下所示:

$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). 您需要指出您正在对Regions对象的特定实例(在上面的示例中$regionsObj )调用getRegionInfo函数。

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. 只需将所有代码放入public方法即可。 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. 第三,您不应该将$db用作全局变量。 Instead, send it as a parameter to the constructor of your Regions class: 而是将其作为参数发送到Regions类的构造函数:

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: 然后像这样创建Regions对象:

$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: 相反,您可以只在$stmt对象上返回fetchAll函数的结果:

 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: 最后一点,我不确定您是否只是不包括这部分代码,而另一个答案中已经提到了这一点,为了完整Regions.php ,请在此包括它,确保包括您的Regions.php文件在引用Regions类之前在index.php

include_once("Regions.php");

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. 如果要从外部调用,请将get_regions()从private更改为public。

How about this.. You need to include_once the file before you can use it 怎么样。.您需要在包含文件之前使用它

index.php index.php

include_once("Regions.php");

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM