PHP中的面向对象编程不明确

I have an issue in understanding Object Oriented Programming in PHP. I believe if i can pass through this a lot of doors would be opened for me. I already know how to write in line php. I have taking a course but i can't still seem to wrap my head around this yet. But here is what i don't understand.

Say i create a php class with functions in it as below.

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

}

From my understanding on the course i took, I know i can make use of these functions as below

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

So my question here is, Is it possible to convert the php in the html file into another class and methods or functions? If so how? Understanding this would help me a lot in my understanding of Object Oriented Programming in PHP.

Thanks

What you're thinking of is a "view" layer.

This layer is typically not a class itself, it's usually just a PHP file with access to functions to help with view related things - like rounding numbers or capitalizing words, etc.

You could take a stab at writing your own template library or use an existing engine like Twig.