首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP,无法在数组中找到对象函数。

PHP,无法在数组中找到对象函数。
EN

Stack Overflow用户
提问于 2015-01-16 16:25:30
回答 1查看 195关注 0票数 1

我正在为一个摄影师建立一个网页,这一页包含了一堆画廊。

我已经将我的页面连接到数据库,我可以从数据库中返回所需的所有内容,并将其放置在图片库对象中。

问题在于:

网页应该能够加载所有的图库,并为每个图库创建一个对象。它应该把它放在一个叫做图库的数组中(这个部分工作得很好)。

接下来,我希望通过getter从对象中获取数据,但是当我尝试这样做时,我会得到以下错误:

致命错误:在第21'行调用C:\xampp\htdocs\index.php中的未定义函数getTitle()

我被困在这一点上,有没有人有解决的办法?

顺便说一句,这是我的代码:

index.php

代码语言:javascript
复制
<?php

    // REQUIRED FILES

    require_once('PHPclasses/database.class.php');


    $database = new Database('localhost','root','','localdatabase');

    include('PHPclasses/gallery.class.php');

    $results = $database->doQuery("SELECT * FROM gallery");

    $galleries = array();

    for($i = 0; $i < count($results); $i++) {
        $galleries[$i] = new Gallery($i+1, $database);
    }

    foreach($galleries as $gallery) {
        $gallery.getTitle();
    }
?>

database.class.php

代码语言:javascript
复制
<?php

/*
 * This class contains the connection with the database.
 *
 * The database connection will be made when instantiating a new database object via 'new Database()'.
 * This is done by providing the 4 parameters:
 *      - server host
 *      - database user
 *      - database password
 *      - database name
 *
 * The database object can be used to input a query via the doQuery method.
 * This method needs the SQL query as a String, It will return an 2D array, being filled with result index as first 
 *      and the database-table contents s second.
 */

class Database 
{
    // Database connection variables
    private $serverhost;
    private $username;
    private $password;
    private $database;

    // Database connection itself
    private $db_link;

    // Query results
    private $resultsArray;

    public function __construct($host, $user, $pass, $db) {

        $this->serverhost = $host;
        $this->username = $user;
        $this->password = $pass;
        $this->database = $db;

        // Create connection
        $this->db_link = new mysqli(
                $this->serverhost,
                $this->username,
                $this->password,
                $this->database
        );

        // Check for errors in connection
        if ($this->db_link->connect_error) {
            die("Connection failed: " . $this->db_link->connect_error);
        }
    }

    public function doQuery($query){
        $q = $this->db_link->query($query)
            or die("Error: ".mysqli_error($this->db_link));

        $i = 0;
        $resultsArray = array();

        while($row = mysqli_fetch_array( $q )) { 
            $resultsArray[$i] = $row;
            $i++;
        }

        return $resultsArray;   
    }
}

?>

最后但并非最不重要的是gallery.class.php

代码语言:javascript
复制
<?php

/*
 * This class contains the Gallery
 *
 * The gallery object is one instance of the complete gallery. 
 * The ID given in the constructor is the ID within the database,
 * 
 * This row in the database contains all the properties for each object of gallery
 *
 * All properties set in the constructor can later be obtained via the getters.
 */

class Gallery
{

    // location of the gallery folder
    private $root;

    // descriptive variables
    private $title;
    private $description;
    private $thumb;
    private $genre;
    private $day;

    /**
     * Constructor if this class
     * 
     * Takes in an identifier which should be the ID of the gallery in the database
     *      and an database object, which should be the database which stores all the data
     */
    public function __construct($galleryIdentifier, $database) {
        $result = $database->doQuery("SELECT * FROM  `gallery` WHERE `galleryID` = '{$galleryIdentifier}'");

        $this->root = $result[0]["galleryRoot"];

        $this->title = $result[0]["title"];
        $this->description = $result[0]["description"];
        $this->thumb = $result[0]["galleryThumb"];
        $this->genre = $result[0]["genre"];
        $this->day = $result[0]["galleryRoot"];
    }

    // list of getters (start)
    public function getRoot() {
        return $this->root;
    }
    public function getTitle() {
        return $this->title;
    }
    public function getDescription() {
        return $this->description;
    }
    public function getThumb() {
        return $this->thumb;
    }
    public function getGenre() {
        return $this->genre;
    }
    public function getDate() {
        return $this->day;
    }
    // list of getters (end)
}

?>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-16 16:28:32

你做了个错字。修正您的方法调用

代码语言:javascript
复制
$gallery->getTitle();
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27988350

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档