得到";未定义的变量“;返回方法


Getting "Undefined variable" in PHP for return method

我用PHP编写了一个助手类。它有一个构造函数,用于初始化将在方法中填充的数组。有两种方法,一种是填充数组,另一种是返回数组。问题是,当我在另一个网页中创建对象并调用方法时,get方法会返回一个错误。我得到这个错误。请帮助

注意:未定义的变量:"C/…."中的startlatitude

<?php 
session_start();
class TrafficData
{
    function __construct()
    {
        $startlatitude = array();
        $startlongitude = array();
        $endlatitude = array();
        $endlongitude = array();
        $delay = array();
        $accidenttype = array();        
     }
    function getdatabyseverity($dayofweek, $zipcode)
    {
        $host="localhost"; // Host name
        $username="kush"; // Mysql username
        $password="password"; // Mysql password
        $db_name="trafficprediction"; // Database name
        $tbl_name="livecongestion"; // Table name
       // Create connection
       $conn = new mysqli($host, $username, $password, $db_name);
       // Check connection
       if ($conn->connect_error)
       {
           die("Connection failed: " . $conn->connect_error);
        }

       $sql = "SELECT `Street`, `CongestionType` , `StartLatitude`, `StartLongitude`, `EndLatitude`, `EndLongitude`, `Delay` FROM `" . $tbl_name . "` WHERE `ZipCode`='"" .$zipcode . "'" and `Day`='"" .$dayofweek."'"";
       $result = $conn->query($sql);
        if ($result->num_rows > 0)
        {
            // output data of each row
            while($row = $result->fetch_assoc())
            {
                $startlatitude[] = $row["StartLatitude"];
                $startlongitude[] = $row["StartLongitude"];
                $endlatitude[] = $row["EndLatitude"];
                $endlongitude[] = $row["EndLongitude"];
                $delay[] = $row["Delay"];
                $accidenttype[] = $row["CongestionType"];
            }
        } else
        {
                echo "0 results";
         }
    }`
    function getarray($text)
    {
        if($text == "START_LAT")
        {
            return $startlatitude;
         }
     }

现在,下面的代码就是我用来创建对象的代码。

<?php 
 include('DataClass.php');
function getdata()
{
    $zipcode=$_POST['zipcode'];
    $dayofweek=$_POST['day'];
    $severity=$_POST['severity'];
    $dataobject = new TrafficData();
    $dataobject->getdatabyseverity($dayofweek, $zipcode);
    $startlatitude = array();
    $startlatitude = $dataobject->getarray("START_LAT");
}

创建object的代码很好。但您并没有返回类变量,而是函数getarray()尝试返回局部变量$startlatitude,这在函数范围内是不可用的。

我想,您忘记了$this来访问类变量。OOPS概念!

<?php 
session_start();
class TrafficData
{
    public var $startlatitude;
    function __construct()
    {
        $this->startlatitude = array();
        $startlongitude = array();
        $endlatitude = array();
        $endlongitude = array();
        $delay = array();
        $accidenttype = array();        
     }
    function getdatabyseverity($dayofweek, $zipcode)
    {
        $host="localhost"; // Host name
        $username="kush"; // Mysql username
        $password="password"; // Mysql password
        $db_name="trafficprediction"; // Database name
        $tbl_name="livecongestion"; // Table name
       // Create connection
       $conn = new mysqli($host, $username, $password, $db_name);
       // Check connection
       if ($conn->connect_error)
       {
           die("Connection failed: " . $conn->connect_error);
        }

       $sql = "SELECT `Street`, `CongestionType` , `StartLatitude`, `StartLongitude`, `EndLatitude`, `EndLongitude`, `Delay` FROM `" . $tbl_name . "` WHERE `ZipCode`='"" .$zipcode . "'" and `Day`='"" .$dayofweek."'"";
       $result = $conn->query($sql);
        if ($result->num_rows > 0)
        {
            // output data of each row
            while($row = $result->fetch_assoc())
            {
                $this->startlatitude[] = $row["StartLatitude"];
                $startlongitude[] = $row["StartLongitude"];
                $endlatitude[] = $row["EndLatitude"];
                $endlongitude[] = $row["EndLongitude"];
                $delay[] = $row["Delay"];
                $accidenttype[] = $row["CongestionType"];
            }
        } else
        {
                echo "0 results";
         }
    }`
    function getarray($text)
    {
        if($text == "START_LAT")
        {
            return $this->startlatitude;
         }
     }