简体   繁体   English

在PHP类中包含另一个文件

[英]Include another file in PHP class

I'm sure this has been asked a few times, but I can't seem to find a straight forward or well explained answer. 我敢肯定这已经被问过几次了,但是我似乎找不到一个简单明了的答案。 I'm attempting to create a Account Object/Class within PHP, where I can retrieve data from an account. 我正在尝试在PHP中创建一个帐户对象/类,在这里我可以从一个帐户中检索数据。 The issue I'm having is that I cannot include my database.php class so I can retrieve all the created accounts. 我遇到的问题是我无法包含我的database.php类,因此我可以检索所有创建的帐户。

I'm fairly new with PHP and don't have an expansive knowledge on how a lot of the processes or conventions work. 我对PHP相当陌生,对很多流程或约定的工作原理也不了解。 The error/warning I'm running into is this; 我遇到的错误/警告是这样;

Warning: include(../database.php): failed to open stream: No such file or directory in ~/account.php on line 3 警告:include(../ database.php):无法打开流:第3行的〜/ account.php中没有此类文件或目录

Here is my account.php class 这是我的account.php类

<?php

include '../database.php';

class Account {

    protected $username, $email;

    protected function getEmail() {
        return $this->email;
    }

    protected function getUsername() {
        return $this->username;
    }

    public function __construct($username, $email) {
        $this->username = $username;
        $this->email = $email;
    }

    public static function getUser($username) {
        $statement = getConnection()->prepare('SELECT * FROM account WHERE username=?');
        $statement->bindValue(1, $username);

        if ($rows = $statement->fetch()) {
             $account = new Account($rows['username'], $rows['email']);
        }

         return $account;
    }

}

Here is my database.php incase I've done something wrong or may need to change things 这是我的database.php,以防万一我做错了或可能需要更改

<?php

function getConnection() {
    $dbh = new PDO("mysql:host=127.0.0.1;dbname=mcsl;port3306", "root", "");
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
    $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    return $dbh;
}

Directory Structure: 目录结构:

目录结构

您可以使用include (dirname(__FILE__)."/../database.php")访问account.php文件中的database.php文件。

You currently have a mix of object-oriented and procedural code. 当前,您混合了面向对象和过程代码。

Adding database features to a class is normally accomplished by dependency injection. 将数据库功能添加到类通常是通过依赖项注入来完成的。 In other words, you pass the database object as argument to whatever method needs it and use type hinting to enforce it, eg: 换句话说,您将数据库对象作为参数传递给需要它的任何方法,并使用类型提示来强制执行它,例如:

public static function getUser(MyConnectionClassOrInterface $db, $username) {
}

As about your include, you're using relative paths, which are relative to main script , not current file. 关于include,您正在使用相对路径,这些路径是相对于main脚本而不是当前文件的。 You can build absolute paths or use a more elaborate class auto-loader. 您可以构建绝对路径,也可以使用更精细的类自动加载器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM