在异常处理方面需要帮助 [英] need help in exception handling
本文介绍了在异常处理方面需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在我的用户类中使用异常。当我抓住它们时,如果我将字段留空,则会自动显示消息并且表单将转到我的标题。我该怎么办 ???
这是我的类代码:
<? php
class usernew1
{
< span class =code-summarycomment> private $ firstName;
private $ middleName;
< span class =code-summarycomment> public function __ construct()
{
}
public function __ set($ name,$ value)
{
$ method = set _ 。$ value;
if(!method_exists($ this, $ method))
{
throw new 异常(property < span class =code-summarycomment> 不 存在 ...);
< span class =code-summarycomment> }
$ this - > $ method($ value);
}
公共函数__get($ name)
{
$ method =get _。$ name;
if(!method_exists($ this,$ method))
{
抛出新的异常(属性不存在......);
}
返回$ this-> $ method();
}
私人函数set_firstName($ firstName)
{
$ reg =/ ^ [a-z] + $ / i;
if(!preg_match($ reg,$ firstName))
{
抛出新的异常(缺少/无效的名字......);
}
$ this-> firstName = $ firstName;
}
私函数get_firstName()
{
返回$ this-> firstName;
}
}
?>
接下来是我的流程页面来捕获异常
流程:
<? php
require_once(usernew1.php);
< span class =code-summarycomment> session_start();
$ objuser = new usernew1();
< span class =code-summarycomment>
$ errors = array();
尝试
{
$ objuser - > __ set(firstName,$ _ POST ['firstName']);
}
catch(Exception $ ex)
{
$ errors ['firstName'] = $ ex-> getMessage();
}
?>
和最终形式在这里.....
<? php
// require_once(webinterface.php);
require_once(usernew1.php);
session_start();
if(isset($ _ SESSION ['errors']))
{
$ errors = $ _ SESSION [' errors'];
}
else if(isset($ _ SESSION ['objuser']))
{
$ objuser = unserialize($ _ SESSION [ ' objuser']);
}
< span class =code-summarycomment> else
{
$ objuser = new usernew1();
}
? >
< !DOCTYPE html PUBLIC - // W3C // DTD XHTML 1.0 Transitional // EN http://www.w3.org/TR/xhtml1/DTD /xhtml1-transitional.dtd\">
& lt; html xmlns =http://www.w3.org/1999 / XHTML>
& lt; head>
& lt; meta http-equiv =Content-Typecontent =text / html; charset = utf-8/>
& lt; title>用户注册& lt; / title>
< link < span class =code-attribute> href = styles / newUserStyles.css type = text / css media = all rel = 样式表 < span class =code-keyword> / >
& lt; / head>
& lt; body>
< div < span class =code-attribute> id = formContainer >
< div id = headingRow > 新用户注册< / div >
& lt; form action =process.phpmethod =postenctype =multipart / form-dataid =userForm>
< div class = row >
< div class = cell cellLeft > 名字< / div >
< < span class =code-leadattribute> div class = cell cellRight >
& lt; input type =textid =firstNamename =firstNamevalue =/> < span id = firstNameError >
< ; php
if(isset($ errors ['firstName']))
{
< span class =code-summarycomment> echo($ errors ['firstName']);
}
? > < / span > < / div > < div class = clearBox > < / div >
< div class = clearBox > < / div >
< div class = row c2 style = text-align:center; > ;
& lt; input type =submitvalue =Register/> < / div >
& lt; / form>
< / div >
& lt; / body>
& lt; / html>
请帮助我并在这里指出我的错误。我是php新手。
提前感谢
解决方案
firstName;
private
middleName;
< span class =code-summarycomment> public function __ construct()
{
}
public function __ set(
名,
i am using exceptions in my user class .when i catch them the message is automatically displayed and form goes to my header if i leave fields empty . what should i do ???
here is my class code :
<?php
class usernew1
{
private $firstName;
private $middleName;
public function __construct()
{
}
public function __set($name,$value)
{
$method = "set_".$value;
if(!method_exists($this, $method))
{
throw new Exception("property does not exist ...");
}
$this->$method($value);
}
public function __get($name)
{
$method = "get_".$name;
if(!method_exists($this,$method))
{
throw new Exception("property does not exist...");
}
return $this->$method();
}
private function set_firstName($firstName)
{
$reg = "/^[a-z]+$/i";
if(!preg_match($reg, $firstName))
{
throw new Exception("missing /invalid first name ....");
}
$this->firstName =$firstName;
}
private function get_firstName()
{
return $this->firstName;
}
}
?>
next is my process page to catch exceptions
process:
<?php
require_once("usernew1.php");
session_start();
$objuser = new usernew1();
$errors = array();
try
{
$objuser->__set("firstName",$_POST['firstName']);
}
catch(Exception $ex)
{
$errors['firstName'] = $ex->getMessage();
}
?>
and in the end form is here .....
<?php
//require_once("webinterface.php");
require_once("usernew1.php");
session_start();
if(isset($_SESSION['errors']))
{
$errors = $_SESSION['errors'];
}
else if(isset($_SESSION['objuser']))
{
$objuser = unserialize($_SESSION['objuser']);
}
else
{
$objuser = new usernew1();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>User Registration</title>
<link href="styles/newUserStyles.css" type="text/css" media="all" rel="stylesheet" />
</head>
<body>
<div id="formContainer">
<div id="headingRow">New User Registration</div>
<form action="process.php" method="post" enctype="multipart/form-data" id="userForm">
<div class="row">
<div class="cell cellLeft">First Name</div>
<div class="cell cellRight">
<input type="text" id="firstName" name="firstName" value=""/><span id="firstNameError">
<?php
if(isset($errors['firstName']))
{
echo($errors['firstName']);
}
?></span></div><div class="clearBox"></div>
<div class="clearBox"></div>
<div class="row c2" style="text-align:center;">
<input type="submit" value="Register" /></div>
</form>
</div>
</body>
</html>
kindly help me and poin out my mistakes here . i am new in php.
thanks in advance
解决方案
firstName; private
middleName; public function __construct() { } public function __set(
name,
这篇关于在异常处理方面需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文