Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP,在数组中找不到对象的函数_Php_Arrays_Oop_Fatal Error - Fatal编程技术网

PHP,在数组中找不到对象的函数

PHP,在数组中找不到对象的函数,php,arrays,oop,fatal-error,Php,Arrays,Oop,Fatal Error,我正在为一位摄影师建立一个网页,这个网页包含一堆画廊 我已经将我的页面连接到数据库,我可以从数据库返回我需要的所有内容,并将其放置在Gallery对象中 问题就在这里: 该网页应该能够加载所有的画廊,并为每个画廊创建一个画廊对象。它应该将其放置在一个名为“库”的数组中(不过这部分效果很好) 接下来,我想通过getter从对象获取数据,但当我尝试此操作时,会出现以下错误: 致命错误:在中调用未定义的函数getTitle() 第21行的C:\xampp\htdocs\index.php' 我被困在这

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

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

问题就在这里:
该网页应该能够加载所有的画廊,并为每个画廊创建一个画廊对象。它应该将其放置在一个名为“库”的数组中(不过这部分效果很好)

接下来,我想通过getter从对象获取数据,但当我尝试此操作时,会出现以下错误:

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

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

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

index.php

<?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

<?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;   
    }
}

?>
<?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)
}

?>

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

<?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;   
    }
}

?>
<?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)
}

?>
你打错了。
修复对的方法调用

$gallery->getTitle();

您有一个输入错误,正确的是
$gallery->getTitle()谢谢您的回复。我花了半年时间编写java,把事情搞混了。又来了@我不明白为什么人们会在评论中发布答案;)@这个问题问得好,可能是我自己搞错了,也可能是我有点怪。原来如此:)我下次写答案时会记住这一点。另外,(与您的问题无关)与其执行第二个循环,为什么不调用$galleries[$I]->getTitle();在您的for(…)循环中?我这样做是为了测试是否得到返回值。稍后在这个项目中,我将在页面加载后立即创建对象,但只在请求后需要数据。然后我得把事情分开。然而,你是对的,在这一时刻它会变得更有意义。那么,如果托马斯兹的答案对你来说一切都有效,请接受它。@Marcel你可以接受答案,通过勾选答案左边的复选标记,你就不必编辑你的标题了