search
HomeBackend DevelopmentPHP TutorialDetailed explanation of the steps to implement guestbook based on object-oriented PHP

这次给大家带来PHP基于面向对象实现留言本步骤详解,PHP基于面向对象实现留言本的注意事项有哪些,下面就是实战案例,一起来看一下。

要设计一留言本,一切都将以留言本为核心,抓到什么是什么,按流程走下来,即按用户填写信息->留言->展示的流程进行。

现在用面向对象的思维思考这个问题,在面向对象的世界,会想尽办法把肉眼能看见的以及看不见的,但是实际存在的物或者流程抽象出来。既然是留言本,那么就存在留言内容这个实体,这个留言实体(domain)应该包括留言者姓名、E-mail、留言内容等要素,如下面代码所示

//留言实体类
class message
{
  public $name;//留言者姓名
  public $email;//留言者联系方式
  public $content;//留言内容
  public function set($name,$value)
  {
    $this->$name = $value;
  }
  public function get($name)
  {
    if(!isset($this->$name))
    {
      $this->$name = NULL;
    }
  }
}

上面的类就是所说的domain,是一个真实存在的、经过抽象的实体模型。然后需要一个留言本模型,这个留言本模型包括留言本的基本属性和基本操作,代码如下所示

class gbookModel
{
  private $bookPath;//留言本文件
  private $data;//留言数据
  public function setBookPath($bookPath)
  {
    $this->bookPath = $bookPath;
  }
  public function getBookPath()
  {
    return $this->bookPath;
  }
  public function open()
  {
  }
  public function close()
  {
  }
  public function read()
  {
    return file_get_contents($this->bookPath);
  }
  //写入留言
  public function write($data)
  {
    $this->data = self::safe($data)->name."&".self::safe($data)->email." said: ".self::safe($data)->content.PHP_EOL;
    return file_put_contents($this->bookPath,$this->data,FILE_APPEND);
  }
  //模拟数据的安全处理,先拆包再打包
  public static function safe($data)
  {
    $reflect = new ReflectionObject($data);
    $props = $reflect->getProperties();
    $messagebox = new stdClass();
    foreach($props as $props)
    {
      $ivar = $props->getName();
      $messagebox->$ivar = trim($props->getValue($data));
    }
    return $messagebox;
  }
  public function delete()
  {
    file_put_contents($this->bookPath,"it's empty now");
  }
}

实际留言的过程可能会更复杂,可能还包括一系列准备操作以及log处理,所以应定义一个类负责数据的逻辑处理,代码如下所示

class leaveModel
{
  public function write(gbookModel $gb, $data)
  {
    $book = $gb->getBookPath();
    $gb->write($data);
    //记录日志
  }
}

最后,通过一个控制器,负责对各种操作的封装,这个控制器是直接面向用户的,所以包括留言本查看、删除、留言等功能。可以形象理解为这个控制器就是留言本所提供的直接面向使用者的功能,封装了操作细节,只需要调用控制器的相应方法即可,代码如下所示

class authorControl
{
  public function message(leaveModel $l, gbookModel $g, message $data)
  {
    //在留言本上留言
    $l->write($g,$data);
  }
  public function view(gbookModel $g)
  {
    //查看留言本内容
    return $g->read();
  }
  public function delete(gbookModel $g)
  {
    $g->delete();
    echo self::view($g);
  }
}

测试代码如下所示

$message = new message;
$message->name = 'chenqionghe';
$message->email = 'cqh@cqh.net';
$message->content = 'chenqionghe is a handson boy.';
$gb = new authorControl();//新建一个留言相关的控制器
$pen = new leaveModel();//拿出笔
$book = new gbookModel();//翻出笔记本
$book->setBookPath("E:\\chenqionghe.txt");
$gb->message($pen,$book,$message);
echo $gb->view($book);
//$gb->delete($book);

这样看起来是不是比面向对象过程要复杂多了?确实是复杂了,代码量增多了,也难理解 。似乎体现不出优点来。但是你思考过以下问题吗?

1.如果让很多人来负责完善这个留言本,一部分实体关系的建立,一部人负责数据操作层的代码,这样是不是更容易分工了呢?

2.如果我要把这个留言本进一步开发,实现记录在数据库中,或者添加分页功能,又该如何呢?

要实现上面的第二问题提出的功能,只需在gookModel类中添加分页方法,代码如下所示

public function readByPage()
{
    $handle = file($this->bookPath);
    $count = count($handle);
    $page = isset($_GET['page']) ? intval($_GET['page']) : 1;
    if($page$count)
      $page = 1;
    $pnum = 9;
    $begin = ($page-1) * $pnum;
    $end = ($begin+$pnum) > $count ? $count :$begin + $pnum;
    for($i=$begin; $i',$i+1,'',$handle[$i],'<br>';
    }
    for($i=1;$i<ceil>'.$i.'';
    }
}</ceil>

然后到前端控制器里添加对应的action,代码如下所示

public function viewByPage(gbookModel $g)
{
    return $g->readByPage();
}

运行结果如下

只需要这么简单的两步,就可以实现所需要的分页功能,而且已有的方法都不用修改,只需要在相关类中新增方法即可。当然,这个分页在实际点击时是有问题的,因为没有把Action分开,而是通通放在一个页面里。对照着上面的思路,还可以把留言本扩展为MySQL数据库的。

这个程序只体现了非常简单的设计模式,这个程序还有许多要改进的地方,每个程序员心中都有一个自己的OO。项目越大越能体现模块划分、面向对象的好处。

下面是完整的代码

$name = $value;
  }
  public function get($name)
  {
    if(!isset($this->$name))
    {
      $this->$name = NULL;
    }
  }
}
/**
 * 留言本模型,负责管理留言本
 * $bookpath:留言本属性
 */
class gbookModel
{
  private $bookPath;//留言本文件
  private $data;//留言数据
  public function setBookPath($bookPath)
  {
    $this->bookPath = $bookPath;
  }
  public function getBookPath()
  {
    return $this->bookPath;
  }
  public function open()
  {
  }
  public function close()
  {
  }
  public function read()
  {
    return file_get_contents($this->bookPath);
  }
  public function readByPage()
  {
    $handle = file($this->bookPath);
    $count = count($handle);
    $page = isset($_GET['page']) ? intval($_GET['page']) : 1;
    if($page$count)
      $page = 1;
    $pnum = 9;
    $begin = ($page-1) * $pnum;
    $end = ($begin+$pnum) > $count ? $count :$begin + $pnum;
    for($i=$begin; $i',$i+1,'',$handle[$i],'
';     }     for($i=1;$i'.$i.'';     }   }   //写入留言   public function write($data)   {     $this->data = self::safe($data)->name."&".self::safe($data)->email." said: ".self::safe($data)->content.PHP_EOL;     return file_put_contents($this->bookPath,$this->data,FILE_APPEND);   }   //模拟数据的安全处理,先拆包再打包   public static function safe($data)   {     $reflect = new ReflectionObject($data);     $props = $reflect->getProperties();     $messagebox = new stdClass();     foreach($props as $props)     {       $ivar = $props->getName();       $messagebox->$ivar = trim($props->getValue($data));     }     return $messagebox;   }   public function delete()   {     file_put_contents($this->bookPath,"it's empty now");   } } class leaveModel {   public function write(gbookModel $gb, $data)   {     $book = $gb->getBookPath();     $gb->write($data);     //记录日志   } } class authorControl {   public function message(leaveModel $l, gbookModel $g, message $data)   {     //在留言本上留言     $l->write($g,$data);   }   public function view(gbookModel $g)   {     //查看留言本内容     return $g->read();   }   public function viewByPage(gbookModel $g)   {     return $g->readByPage();   }   public function delete(gbookModel $g)   {     $g->delete();     echo self::view($g);   } } $message = new message; $message->name = 'chenqionghe'; $message->email = 'cqh@cqh.net'; $message->content = 'chenqionghe is a handson boy.'; $gb = new authorControl();//新建一个留言相关的控制器 $pen = new leaveModel();//拿出笔 $book = new gbookModel();//翻出笔记本 $book->setBookPath("E:\\chenqionghe.txt"); $gb->message($pen,$book,$message); //echo $gb->view($book); echo $gb->viewByPage($book); //$gb->delete($book);

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

PHP依赖倒置案例详解

Bootstrap+PHP实现多图上传步骤详解

The above is the detailed content of Detailed explanation of the steps to implement guestbook based on object-oriented PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What are some common problems that can cause PHP sessions to fail?What are some common problems that can cause PHP sessions to fail?Apr 25, 2025 am 12:16 AM

Reasons for PHPSession failure include configuration errors, cookie issues, and session expiration. 1. Configuration error: Check and set the correct session.save_path. 2.Cookie problem: Make sure the cookie is set correctly. 3.Session expires: Adjust session.gc_maxlifetime value to extend session time.

How do you debug session-related issues in PHP?How do you debug session-related issues in PHP?Apr 25, 2025 am 12:12 AM

Methods to debug session problems in PHP include: 1. Check whether the session is started correctly; 2. Verify the delivery of the session ID; 3. Check the storage and reading of session data; 4. Check the server configuration. By outputting session ID and data, viewing session file content, etc., you can effectively diagnose and solve session-related problems.

What happens if session_start() is called multiple times?What happens if session_start() is called multiple times?Apr 25, 2025 am 12:06 AM

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

How do you configure the session lifetime in PHP?How do you configure the session lifetime in PHP?Apr 25, 2025 am 12:05 AM

Configuring the session lifecycle in PHP can be achieved by setting session.gc_maxlifetime and session.cookie_lifetime. 1) session.gc_maxlifetime controls the survival time of server-side session data, 2) session.cookie_lifetime controls the life cycle of client cookies. When set to 0, the cookie expires when the browser is closed.

What are the advantages of using a database to store sessions?What are the advantages of using a database to store sessions?Apr 24, 2025 am 12:16 AM

The main advantages of using database storage sessions include persistence, scalability, and security. 1. Persistence: Even if the server restarts, the session data can remain unchanged. 2. Scalability: Applicable to distributed systems, ensuring that session data is synchronized between multiple servers. 3. Security: The database provides encrypted storage to protect sensitive information.

How do you implement custom session handling in PHP?How do you implement custom session handling in PHP?Apr 24, 2025 am 12:16 AM

Implementing custom session processing in PHP can be done by implementing the SessionHandlerInterface interface. The specific steps include: 1) Creating a class that implements SessionHandlerInterface, such as CustomSessionHandler; 2) Rewriting methods in the interface (such as open, close, read, write, destroy, gc) to define the life cycle and storage method of session data; 3) Register a custom session processor in a PHP script and start the session. This allows data to be stored in media such as MySQL and Redis to improve performance, security and scalability.

What is a session ID?What is a session ID?Apr 24, 2025 am 12:13 AM

SessionID is a mechanism used in web applications to track user session status. 1. It is a randomly generated string used to maintain user's identity information during multiple interactions between the user and the server. 2. The server generates and sends it to the client through cookies or URL parameters to help identify and associate these requests in multiple requests of the user. 3. Generation usually uses random algorithms to ensure uniqueness and unpredictability. 4. In actual development, in-memory databases such as Redis can be used to store session data to improve performance and security.

How do you handle sessions in a stateless environment (e.g., API)?How do you handle sessions in a stateless environment (e.g., API)?Apr 24, 2025 am 12:12 AM

Managing sessions in stateless environments such as APIs can be achieved by using JWT or cookies. 1. JWT is suitable for statelessness and scalability, but it is large in size when it comes to big data. 2.Cookies are more traditional and easy to implement, but they need to be configured with caution to ensure security.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor