当前位置:Gxlcms > PHP教程 > (九)面向对象的设计原则之五

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

时间:2021-07-01 10:21:17 帮助过:5人阅读

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

message.php

$name = $value;
    }

    public function __get($name){
        if (!isset($this->$name)){
            $this->$name = null;
        }
    }
}

gbookModel.php

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
getBookPath();
        $gb->write($data);
    }
}
view.php

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 = 'test@test.com';
$message->content = 'love php';
$gb = new authorControl();
$pen = new leaveModel();
$book = new gbookModel();
$book->setBookPath('test.txt');
$gb->message($pen,$book,$message);
echo $gb->view($book);
$gb->delete($book);

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

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

人气教程排行