题目:
(1)设计一个注册表单,表单提交到register.php,提交方法为post,表单有四个文本框控件:user、pwd、mphone和address和提交、重置按钮。写出该表单的html代码
(2)设计register.php文件:
(A)读出表单数据,将pwd用 md5()函数加密
(B)创建一User类,User类有四个私有属性: user、pwd、mphone和address
(C)为每个属性定义公有的get/set方法
(D)定义公有的构造方法
(E)根据用户表单的输入,生成一个相应的User对象
参考代码:
前端页面
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Document</title> <style> .reg_form{ padding: 20px; width: 500px; margin: 0 auto; border: 1px solid blue; border-radius:10px; } .reg_form label{ width: 70px; display: inline-block; } .reg_form input{ margin-top:20px; } </style> </head> <body> <form action="Register.php" method="post" class="reg_form"> <label for="user">用户名:</label> <input type="text" id="user" name="user" required /><br> <label for="pwd">密码:</label> <input type="password" id="pwd" name="pwd" required/><br> <label for="mphone">手机:</label> <input type="tel" name="mphone" id="mphone" required/><br> <label for="address">地址:</label> <input type="text" name="address" id="address" required/><br> <input type="submit" value="注册" /> <input type="reset" value="重置" /> </form> </body> </html>
效果图:
php代码:
<?php header("content-type:text/html;charset=utf-8"); $user_post=addslashes(trim($_POST['user'])); $pwd_post=md5($_POST['pwd']); $mphone_post=addslashes(trim($_POST['mphone'])); //addslashes转移字符过滤 $address_post=addslashes(trim($_POST['address'])); //trim两边空格过滤 class User { private $user; private $pwd; //私有属性 private $mphone; private $address; function __construct($user,$pwd,$mphone,$address) //构造方法 { $this->user=$user; $this->pwd=$pwd; $this->mphone=$mphone; $this->address=$address; } function __GET($info) //魔术方法 { return $this->$info; } function __SET($info,$value) //魔术方法 { $this->$info=$value; } } $ansion=new User($user_post,$pwd_post,$mphone_post,$address_post); //新建User对象 echo $ansion->user."--".$ansion->pwd."--".$ansion->mphone."--".$ansion->address; //获取私有属性值
代码仅供参考,如有错漏,欢迎指出
来自Anson博客
2016年6月27日
--------------------2016年6月30日 更新---------------
看到有些朋友的类的get/set方法写成如下:
class User { private $user; private $pwd; private $mphone; private $address; function __construct($user,$pwd,$mphone,$address) { $this->user=$user; $this->pwd=$pwd; $this->mphone=$mphone; $this->address=$address; } function setUser($user) { $this->user=$user; } function getUser() { return $this->user; } function setPwd($pwd) { $this->addpwd=$pwd; } function getPwd() { return $this->pwd; } function setMphone($mphone) { $this->mphone=$mphone; } public function getMphone() { return $this->mphone; } function setAddress($address) { $this->adress=$address; } function getAddress() { return $this->address; } }
可能是因为在老师的ppt笔记中有这样一段话:
在类的方法外,如何存取该类对象的private属性呢?
答案是,在类中为private属性定义相应的、public型的get/set方法,通过这两个public方法来间接存取对象的private属性
// get/set方法是类中为private、protected属性专门设计的public型的方法: public function setXxx($xxx) { $this->xxx=$xxx; } public function getXxx() { return $this->xxx; }
这样写也是正确的,在类外面调用私有属性的时候,要通过调用类里面的公共方法,间接获取私有属性值。
$name=$ansion->getName(); $pwd=$ansion->getPwd(); $ansion->setName('ansion');
但是重复写get、set方法有点繁琐,我将它们缩写成如下两个方法:
function GET($info){ return $this->$info; } function SET($info,$value){ $this->$info=$value; }
在类外面调用的时候就可以这样写:
$ansion->GET(name); $ansion->GET(pwd); $ansion->SET('name','ansion'); //这样就不用重复为每个属性写get、set方法
这时候就又回到了一开始的魔术方法__GET,__SET方法了,魔术方法与上面不同的是会自动调用相应的属性值,我们就可以直接这样写:
$ansion->name; $ansion->pwd; $ansion->name="ansion";
总结:鉴于题目(C)为每个属性定义公有的get/set方法,我觉得为每个属性分开写get、set方法会更加符合题意。