PHP中的面向对象编程尚不清楚

时间:2018-07-19 16:58:04

标签: php mysql pdo

在了解PHP中的面向对象编程方面,我遇到了一个问题。我相信,如果我能通过这个大门,将会为我打开许多大门。我已经知道如何在php行中写代码。我正在上一门课程,但似乎仍无法解决这个问题。但这是我不明白的。

假设我创建了一个php class,其中包含function,如下所示。

Database.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转换为另一个classmethodsfunction个文件?如果可以,怎么办?理解这一点将有助于我对PHP中的面向对象编程的理解。

谢谢

1 个答案:

答案 0 :(得分:0)

您要考虑的是“视图”层。

该层通常不是类本身,通常只是一个PHP文件,可以访问功能来帮助查看与视图相关的内容,例如四舍五入数字或大写单词等。

您可以尝试编写自己的模板库,也可以使用Twig之类的现有引擎。