首页 > 解决方案 > PHP中的面向对象编程不清楚

问题描述

我在理解PHP. 我相信如果我能通过这里,很多门都会为我打开。我已经知道怎么写行了php。我参加了一门课程,但我似乎仍然无法解决这个问题。但这是我不明白的。

假设我在其中创建了一个php class带有functions 的内容,如下所示。

数据库.php

<?php

/*Contains DB Connect function and other query functions that has to do with database*/

class Database 
{
    //Database conn properties

    private $host   = 'localhost';
    private $user   = 'root';
    private $pass   = 'password';
    private $dbname = 'rtmdb';

    private $dbh;
    private $error;
    private $stmt;

    public function __construct() 
    {
        //Function for database connection
        //Set DSN

        $dsn = 'mysql:host='. $this->host . ';dbname'. $this->dbname;

        //Set Options include persistent connection

        $options = array(
            PDO::ATTR_PERSISTENT    => true,
            PDO::ATTR_ERRMODE       => PDO::ERRMODE_EXCEPTION
        );

        //Create new PDO Instance

        try
        { 
            $this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
        }
        catch(PDOException $e) 
        {
            $this->error = $e->getMessage();
        }
    }

    public function query($query)
    {
        //@param function for executing insert, select, update

        $this->stmt = $this->dbh->prepare($query);

        if(!$this->stmt)
        {
            echo $this->dbh->lastErrorMsg();
        }
        else
        {
            return $this->stmt = $this->dbh->prepare($query);
        }
    }

    public function bind($param, $value, $type = null) 
    {
        if(is_null($type))
        {
            switch(true)
            {
                case is_int($value):
                    $type = PDO::PARAM_INT;
                    break;
                case is_bool($value):
                    $type = PDO::PARAM_BOOL;
                    break;
                case is_null($value):
                    $type = PDO::PARAM_NULL;
                    break;
                    default;
                    $type = PDO::PARAM_STR;
            }
        }
        $this->stmt->bindValue($param, $value, $type);
    }

    public function execute()
    {
        return $this->stmt->execute();
    }

    public function lastInsertId() 
    {
        $this->dbh->lastInsertId();
    }

    public function resultset()
    {
        $this->execute();
        return $this->stmt->fetchAll(PDO::FETCH_ASSCO);
    }

}

根据我对所学课程的理解,我知道我可以使用以下function这些

<?php
/*Working With Database*/
/*PHP Data Objects (PDO)*/

require 'classes/Database.php';

$database = new Database;



$post = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);

if($_POST['submit']) {
    $title = $post['title'];
    $body = $post['body'];

    $database->query('INSERT INTO post (title, body) VALUES(:title, :body)');
    $database->bind(':title', $title);
    $database->bind(':body', $body);
    $database->execute();

    if($database->lastInsertId()) {
        echo '<p>Post Added</p>';
    }
}

$database->query('SELECT * FROM post');
// $database->bind(':id', 1);
$rows = $database->resultset();

?>

<h1>Add Post</h1>
<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
    <lable>Post Totle</label>
    <input type="text" name="title" placeholder="Add a Title"><br><br>
    <label>Post Body</label>
   <textarea name="body"></textarea><br><br>
    <input type="submit" name="submit" value="submit">
</form>

<h1>Posts</h1>
<div>
<?php foreach($rows as $row) : ?>
<div>
    <h3><?php echo $row['title']; ?></h3>
    <p><?php echo $row['body'];?></p>
</div>
<?php endforeach;?>

所以我的问题是,是否可以将文件php中的转换html为另一个classandmethodsfunctions?如果有怎么办?理解这一点将有助于我理解PHP.

谢谢

标签: phpmysqlpdo

解决方案


您正在考虑的是“视图”层。

该层通常不是一个类本身,它通常只是一个 PHP 文件,它可以访问帮助查看相关内容的函数 - 例如舍入数字或大写单词等。

您可以尝试编写自己的模板库或使用现有的引擎,例如Twig


推荐阅读