从函数中的函数传递包含文件

时间:2017-10-09 13:42:08

标签: php

我对类非常陌生,并努力将一个简单的函数连接到另一个函数。每当我在ConnectDB函数中添加echo'test'时,它都会通过'test'。但是,它不包括包含文件。

这可能是一个noob错误。

注意:当我将ConnectDB中的内容复制/粘贴到UpdateFollowers函数时,它可以正常工作。我正在寻找一种不使用包含文件来节省时间的方法。

class UpdateAccountInfo {


public function ConnectDB() {

        if (strposa($_SESSION['geturl'], $_SESSION['folders']) != FALSE) {
            include '../_includes/config.php';
            include '../_includes/opendb.php';
        } else {
            include './_includes/config.php';
            include './_includes/opendb.php';
        }   


}

// update followers
public function UpdateFollowers($getid) {

        $this->ConnectDB();

        //find the source of the ig account 
        $sql = "SELECT * FROM ig_accounts WHERE iid = $getid";
        $result = $conn->query($sql);

        if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
            $igname = $row["igname"];
        }
............

1 个答案:

答案 0 :(得分:1)

什么是课程?

在对手头的案例进行任何解释之前,我认为首先获得一些OOP概念非常重要。从问题中的例子来看,我感觉你对类的理解是他们只是"功能桶"可以这么说。事实并非如此。 class是一种通用数据结构,可用于定义对象,与这些对象相关的行为以及这些对象使用的数据。

class Human {

    private $firstName;
    private $lastName;
    private $dateOfBirth;

    public function __construct($fn, $ln, $dob) {
        $this->firstName = $fn;
        $this->lastName = $ln;
        $this->dateOfBirth = $dob;
    }

    public function getFullName() {
        return $this->firstName.' '.$this->lastName;
    }
}

在上面的示例中,我们定义了Human类型的所有对象共享的基本结构以及encapsulated一些非常基本的功能(获取{{1}的全名}})。

如果我们看一下您的示例,Human更多的是功能而不是对象。这是一个始终由系统的其他组件执行的过程,为了本示例的目的,我们将其他组件称为UpdateAccountInfo

Account

现在我们有了一个class Account { private $id; private $name; private $conn; // <-- our DB connection /* other Account properties */ public function __construct($id, $name) { $this->id = $id; $this->name = $name; /* Initialize DB connection */ } public function updateFollowers() { /* Perform any logic required to update the followers */ } } 类型的对象,我们将来添加了许多我们需要的函数。 AccountaddFollowers()removeFollowers()。这些都是属于changeUsername()对象的进程的示例,可能永远不会被系统的任何其他组件使用。

初始化数据库连接

您可能已经注意到我遗漏了上一个示例中的数据库初始化部分。这有几个原因,但主要是:

  1. 每次使用Account关键字创建Account(或任何对象)的实例时,打开和关闭连接都非常昂贵。
  2. 它打破了Single Responsibility Principle。我不太喜欢这个,因为这是一个非常漫长的话题。你暂时需要知道的是,它本质上意味着一个对象应该只处理它自己的数据/进程,而不是更多。在这种情况下,打开数据库连接根本不是new
  3. 的工作

    为了解决这个问题,我们可以简单地将数据库连接逻辑封装在Account对象中。

    Database

    现在您只需要在脚本开头一次初始化数据库连接,并将其传递给可能需要它的其他对象/函数。

    class Database {
    
        private $connection;
    
        public function connect($localhost, $name, $password, $database) {
            $this->connection = new mysqli($localhost, $name, $password, $database);
            if($this->connection->connect_error) {
                /* throw an exception */
            }
        }
    
        public function getConnection() {
            return $this->connection;
        }
    }
    

    如果我们想要修改第二个帐户,我们可以使用刚刚打开的相同数据库连接来执行此操作。

    <?php
        include_once('../../config.php');
        /* include any other required files */
        $database = new Database($config['localhost'], $config['username'], $config['password'], $config['dbname']);
    
        // get a specific account
        $account = new Account(/* ... */, $database);
    
        // update the account's followers
        $account->updateFollowers(/* ... */);
    

    进一步阅读

    1. PHP documentation on OOP
    2. Single Responsibility Principle
    3. 上的维基百科页面
    4. How to initialize a database connection
相关问题