时间:2021-07-01 10:21:17 帮助过:3人阅读
输出的方式测试 echo '###################输出测试################
'.htmlspecialchars($contents).''; ?>
'; if($actionSubmit != null && $actionSubmit != '') { if($reqTheType == '1') { testSearch(); } if($reqTheType == '2') { testInsert(); testSearch(); } if($reqTheType == '3') { testUpdate(); testSearch(); } } /** * 数据库查询 * Enter description here ... */ function testSearch() { echo '查询数据
'; global $hostname,$username,$password,$database,$databaseCharset; $currentConn = null; $currentConn = mysql_connect ( $hostname, $username, $password ); mysql_select_db ( $database ); mysql_query("set names charset ".$databaseCharset); mysql_query("set names ".$databaseCharset); $result = mysql_query ( "select * from e_user" ); //查询动作返回的是result结果集 while ( $row = mysql_fetch_object ( $result ) ) { echo $row->uri . "\t" . ($row->username) . "
"; } mysql_free_result ( $result ); mysql_close ( $currentConn ); } /** * 数据库数据添加 * Enter description here ... */ function testInsert() { global $hostname,$username,$password,$database,$databaseCharset; $insertSql = "insert into e_user(uri,username,password) values"; $insertSql .= "("; $insertSql .= "'".generateId()."','测试用户','123456'"; $insertSql .= ")"; $currentConn = null; $currentConn = mysql_connect ( $hostname, $username, $password ); mysql_select_db ( $database ); mysql_query("set names charset ".$databaseCharset); mysql_query("set names ".$databaseCharset); echo '添加数据'.$insertSql.'
'; $result = mysql_query($insertSql); //插入动作返回的是boolean if(!$result) { die('Error: ' . mysql_error()); } mysql_close ( $currentConn ); } /** * 数据库修改 * Enter description here ... */ function testUpdate() { global $hostname,$username,$password,$database,$databaseCharset; $updateSql = "update e_user"; $updateSql .= " set username='修改后的用户名称' where uri = '001'"; $currentConn = null; $currentConn = mysql_connect ( $hostname, $username, $password ); mysql_select_db ( $database ); mysql_query("set names charset ".$databaseCharset); mysql_query("set names ".$databaseCharset); echo '修改数据'.$updateSql.'
'; $result = mysql_query($updateSql); //插入动作返回的是boolean if(!$result) { die('Error: ' . mysql_error()); } mysql_close ( $currentConn ); } /** * 自动生成ID号 * @param unknown_type $count */ function generateId($count = 6) { $resultId = ''; for($i=0;$i<$count;$i++) { $resultId .= (string)rand(0, 9); } return $resultId; } ?>
uri = $uri; $this->username = $username; $this->password = $password; $this->flag = '100'; $this->type = self::USER_TYPE_NORMAL; } /*测试静态函数的处理*/ static function testStatic() { //$this->username = 'static'; //该方法是错误的,静态方法中只能操作静态变量 return self::USER_TYPE_NORMAL; } /*get set 方法用于管理内部的字段属性*/ public function getUri() { return $this->uri; } public function getUsername() { return $this->username; } public function getPassword() { return $this->password; } public function setUri($uri) { $this->uri = $uri; } public function setUsername($username) { $this->username = $username; } public function setPassword($password) { $this->password = $password; } public function getType() { return $this->type; } public function setType($type) { $this->type = $type; } /*实现底层的抽象方法*/ function showInfo() { echo '我是MyUser对象.'; } //实现接口方法 public function start() { echo '启动MyUser对象....'; } //实现接口方法 public function stop() { echo '停止MyUser对象....'; } } //扩展自MyUser的类 class MyExtendUser extends MyUser implements Module { /*覆盖父类的构造函数*/ function __construct($uri = '',$username = '', $password = '') { //调用父类的构造函数 parent::__construct($uri,$username,$password); //实现自己的一些初始化动作 $this->flag = '200'; } /*覆盖父类的getUsername方法*/ public function getUsername() { return '继承自MyUser,'.$this->username; } //实现接口方法 public function start() { echo '启动MyExtendUser对象....'; } //实现接口方法 public function stop() { echo '停止MyExtendUser对象....'; } } //测试用户对象 $theUserObj = new MyUser('001','测试用户1','123'); echo '用户名称:'.$theUserObj->getUsername().'
'; print_r($theUserObj); echo '
'; echo '测试静态函数1:'.$theUserObj->testStatic().'
'; echo '测试静态函数2:'.MyUser::testStatic().'
'; echo '测试实现的接口:'; $theUserObj->start(); echo '
'; //测试继承 $theUserObj2 = new MyExtendUser('002','测试用户2','123'); echo '用户名称2(继承):'.$theUserObj2->getUsername().'
'; print_r($theUserObj2); echo '
'; echo '测试实现的接口2:'; $theUserObj2->start(); echo '
'; ?>