1. ホーム
  2. php

[解決済み] 未定義のメソッドの呼び出し、修正方法は?[クローズド]。

2022-02-09 07:41:09

質問

PHPで以下のような致命的なエラーが発生します。

致命的なエラーです。Call to undefined method CoffeeModel::GetCoffeeByType() in C:\wampwww12.01.2015 Class 01Coffee WebsiteControllerCoffeeController.php on line 37

Call Stack
#   Time    Memory  Function    Location
1   0.0030  142264  {main}( )   ..\Coffee.php:0
2   0.0130  162800  CoffeeController->CreateCoffeeTables( ) ..\Coffee.php:15

上記エラーのコードを以下に示します。

function CreateCoffeeTables($types)
{
    $coffeeModel = new CoffeeModel();

    $coffeeArray = $coffeeModel->GetCoffeeByType($types);
    $result = "";

    // Generate a coffeeTable for each coffeeEntity in array
    foreach ($coffeeArray as $key => $coffee) {
        $result = $result .
            "<table class = 'coffeeTable'>
            <tr>
            <th rowspan='6' width= '150px' ><img runat = 'server' src = '$coffee->image'/></th>
            <th width = '75px' >Name: </th>
            <td>$coffee->name</td>
            </tr>

            <tr>
            <th>Type: </th>
            <td>$coffee->type</td>
            </tr>

            <tr>
            <th>Price: </th>
            <td>$coffee->price</td>
            </tr>

            <tr>
            <th>Roast: </th>
            <td>$coffee->roast</td>
            </tr>

             <tr>
            <th>Origin: </th>
            <td>$coffee->country</td>
            </tr>

            <tr>
            <td colspan='2' >$coffee->review</td>
            </tr>

             </table>";
    }
    return $result;
}

このコードでは、CoffeeModelクラスが存在します。

<?php

require ("Entities/CoffeeEntity.php");

// contains database related code for the coffee type

class CoffeeModel
{

  //  Get all coffee types from the database and return them in an array

function GetCoffeeTypes()

{

 require 'Credentials.php';

 //Open connection and Select database

 mysql_connect($host, $user, $password) or die (mysql_error());

 mysql_select_db($database);

 $result = mysql_query("SELECT DISTINCT type FROM coffee") or die(mysql_error());

 $types = array();


 // Get data from databse

 while($row = mysql_fetch_array($result))

 {

    array_push($types, $row[0]);


 }

// Close connection and return

  mysql_close();

  return $types;  

}
}

// GET coffeeEntity objects from the database and return them in an array.

function GetCoffeeByType($type)

{

require 'Credentials.php';

// Open connection and select database

mysql_connect($host, $user, $password) or die (mysql_error());

 mysql_select_db($database);

 $query = "SELECT * FROM coffee WHERE LIKE '$type'";

 $result = mysql_query($query) or die (mysql_error());

 $coffeeArray = array();

 //GET Data from Database

 while ($row = mysql_fetch_array($result))

 {

    $name = $row[1];

    $type = $row[2];

    $price = $row[3];

    $roast = $row[4];

    $country = $row[5];

    $image = $row[6];

    $review = $row[7];


    // Create Coffee objects and store them in an array

    $coffee = new CoffeeEntity (-1, $name, $type, $price, $roast, $country, $image, $review);

    array_push($coffeeArray, $coffee);

 }

 // CLose connection and return result

 mysql_close();

 return $coffeeArray;


 }

どうすれば直るのでしょうか?

どのように解決するのですか?

さて、投稿された問題だけに焦点を当てると、あなたの問題は、最初のメソッドの後でクラスを閉じていることです。

}
} <---- this closing bracket is terminating the class

// GET coffeeEntity objects from the database and return them in an array.

function GetCoffeeByType($type)

{

この括弧を1つだけファイルの最後に移動させれば、うまくいくはずです。その次は、クラスの構成をブラッシュアップする必要がありますが、それはまた別の話です。