English 中文(简体)
当我从阵列中找回一个物体时,物体的射线是空的。
原标题:Array of objects is empty when I come to retrieve one from the array
  • 时间:2010-04-13 08:37:42
  •  标签:
  • php
  • mysql
  • oop

我试图从一个数据库中排出,然后从数据库中制造物体,并将这些物体添加到一个私人阵列。

我的班子是:

<?php

include("databaseconnect.php");

class stationItem {
    private $code =   ;
    private $description =   ;


    public function setCode($code ){
        $this->code = $code;
    }

    public function getCode(){
        return $this->code;
    }

    public function setDescription($description){
        $this->description = $description;
    }

    public function getDescription(){
        return $this->description;
    }

}


class stationList {
    private $stationListing;

    function __construct() {
        connect();
        $stationListing = array();

        $result = mysql_query( SELECT * FROM stations );

        while ($row = mysql_fetch_assoc($result)) {
            $station = new stationItem();
            $station->setCode($row[ code ]);
            $station->setDescription($row[ description ]);

            array_push($stationListing, $station);
        }
        mysql_free_result($result);
    }


   public function getStation($index){
        return $stationListing[$index];
   }
}

?>

你可以看到,我正在建立一个站。 每个数据库浏览量(现在有代码和说明)的项目标本,然后我将这些标注推到作为固定站式的私人变量的我的阵列结束。

这部法典规定了这些类别,并试图获取这些类别的财产:

$stations = new stationList();
$station = $stations->getStation(0);
echo $station->getCode();

我发现,施工人员结束时的面积为1,但当我们试图利用指数从阵列中获取物体时,则为零。 因此,我发现的错误是:

致命错误: 要求成员履行非目标的职能

请允许我向我解释为什么发生这种情况? 我猜测,我误解的是,第5号战略中的参考内容如何。

问题回答

提 出

$this->stationListing

(a)

进入班级成员时,你总是必须使用“魔法”()。 参考instance。 注: 查阅static members,如您必须使用:,改为(或static:,从PHP5.3开始,但又引申另一个故事)。

$stationListing in the Constructionor is referencing a local variability, not the one in their category. 修改如下:

function __construct() {
...
$this->stationListing = array();
...
array_push($this->stationListing, $station);




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签