• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

(九)面向对象的设计原则之五

php 搞代码 3年前 (2022-01-23) 14次浏览 已收录 0个评论

一.面向对象的总结:
1.高度抽象有利于高度总结。
2.代码即文档。
二.以面向对象思想做的留言本实例:

message.php

<?php/* * 留言实体类 */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;        }    }}

gbookModel.php

<?php/* * 留言本模型 */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."\r\nsaild:\r\n".self::safe($data)->content;        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 $prop){            $ivar = $prop -> getName();            $messagebox ->$ivar= trim($prop->getValue($data));        }        return $messagebox;    }    public function delete(){        file_put_contents($this->bookPath,'it\'s empty now');    }}

leaveModel.php

<?php//业务逻辑class leaveModel{    public function write(gbookModel $gb,$data){        $book = $gb->getBookPath();        $gb->write($data);    }}

view.php

<?phpinclude "gbookModel.php";include "leaveModel.php";include "message.php";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 = 'phper';$message->email = '[email protected]';$message->content = 'love php';$gb = new authorContr6来源gaodaimacom搞#^代%!码网搞gaodaima代码ol();$pen = new leaveModel();$book = new gbookModel();$book->setBookPath('test.txt');$gb->message($pen,$book,$message);echo $gb->view($book);$gb->delete($book);

版权声明:本文为博主原创文章,未经博主允许不得转载。

以上就介绍了(九)面向对象的设计原则之五,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:(九)面向对象的设计原则之五

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址