面向对象的PHP数组 [英] Object Oriented PHP Arrays

查看:233
本文介绍了面向对象的PHP数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从来没有尝试OO PHP之前,所以我决定做一个简单的CMS,以了解更多信息。我有装载值到一个多维阵列中的问题。

I've never tried OO PHP before so I decided to make a simple CMS to learn more. I am having a problem loading values into a multi-dimensional array.

class Article {
  private $index = 0;
  private $article;

  public function Article() {
   $get_articles = mysql_query("SELECT * FROM `articles`");
   while ($result = mysql_fetch_array($get_articles)) {
    echo $result["article"];

    $this->article[$index]["Tags"] = $result["tags"];
    $this->article[$index]["Categories"] = $result["categories"];
    $this->article[$index]["Date"] = $result["date"];
    $this->article[$index]["Article"] = $result["article"];
    $this->article[$index]["URL"] = $result["url"];

    $index++;
   }
  }

  public function getArticle($articleID) {
   return $this->article[$articleID]["Article"];
  }

  public function getTags($articleNumber) {

  }

  public function getCategories($articleNumber) {

  }

  public function getDate($articleNumber) {

  }
 }

回声$结果[文章] 输出独一无二的文​​章的价值就好了,但显然并没有把它放入数组?

The line echo $result["article"] outputs the one and only article value just fine, but apparently doesn't put it into the array?

$art = new Article();
echo $art->getArticle(0);

这不输出的文章不过。会有人这么好心指出我的错误小白?

This doesn't output the article however. Would someone so kindly point out my noob mistake?

推荐答案

您没有初始化您的数组。

You didn't initialize your array.

$this->article = array();

while ($result = mysql_fetch_array($get_articles)) {
  $this->article[$index] = array();

这篇关于面向对象的PHP数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆