我正在创建一个PHP程序,其中包含一个具有不同属性的学生类,例如名称,ID,地址等。我不明白为什么我的“设置属性”部分中的验证未正确更新。状态,邮政编码和年龄都在更新,但其余部分则没有。我不知道为什么我的学生ID显示274.我甚至没有得到它的地方。请解释为什么我的验证无法正常工作。
function set_student_name($value)
{
$error_message = true;
(ctype_alpha($value) && strlen($value) <= 20) ? $this->student_name = $value : $error_message = false;
return $error_message;
}
这就是我认为的意思: 如果value是一个字符串AND值是&lt; = 20 THEN value == student_name。
好的,这是我的其余代码。有两个文件。第一个就是上课。
class Student
{
// --------------------------------Properties-----------------------------------
private $student_id = 6212;
private $student_name = 'No Name';
private $student_address = 'No Address';
private $student_state = 'No State';
private $student_zip = 0;
private $student_age = 0;
private $error_message = '??';
// --------------------------------Constructor----------------------------------
public function __construct($value1, $value2, $value3, $value4, $value5, $value6)
{
$id_error = $this->set_student_id($value1) == true ? 'TRUE,' : 'FALSE,';
$name_error = $this->set_student_name($value2) == true ? 'TRUE,' : 'FALSE,';
$address_error = $this->set_student_address($value3) == true ? 'TRUE,' : 'FALSE,';
$state_error = $this->set_student_state($value4) == true ? 'TRUE,' : 'FALSE,';
$zip_error = $this->set_student_zip($value5) == true ? 'TRUE,' : 'FALSE,';
$age_error = $this->set_student_age($value6) == true ? 'TRUE,' : 'FALSE,';
$this->$error_message = $id_error . $name_error . $address_error . $state_error . $zip_error . $age_error;
}
// ---------------------------------toString------------------------------------
public function __toString()
{
return $this->$error_message;
}
// --------------------------------Set Methods----------------------------------
public function set_student_id($value)
{
$error_message = true;
(ctype_digit($value) && ($value > 0 && $value <= 9999)) ? $this->student_id = $value : $error_message = false;
return $error_message;
}
public function set_student_name($value)
{
$error_message = true;
(ctype_alpha($value) && strlen($value) <= 20) ? $this->student_name = $value : $error_message = false;
return $error_message;
}
public function set_student_address($value)
{
$error_message = true;
(ctype_alpha($value) && strlen($value) <= 20) ? $this->student_address = $value : $error_message = false;
return $error_message;
}
public function set_student_state($value)
{
$error_message = true;
(ctype_alpha($value) && strlen($value) <= 20) ? $this->student_state = $value : $error_message = false;
return $error_message;
}
public function set_student_zip($value)
{
$error_message = true;
(ctype_digit($value) && ($value > 0 && $value <= 99999)) ? $this->student_zip = $value : $error_message = false;
return $error_message;
}
public function set_student_age($value)
{
$error_message = true;
(ctype_digit($value) && ($value > 0 && $value <= 100)) ? $this->student_age = $value : $error_message = false;
return $error_message;
}
// --------------------------------Get Methods----------------------------------
public function get_student_id()
{
return $this->student_id;
}
public function get_student_name()
{
return $this->student_name;
}
public function get_student_address()
{
return $this->student_address;
}
public function get_student_state()
{
return $this->student_state;
}
public function get_student_zip()
{
return $this->student_zip;
}
public function get_student_age()
{
return $this->student_age;
}
public function get_properties()
{
return "$this->student_id, $this->student_name, $this->student_address, $this->student_state, $this->student_zip, $this->student_age.";
}
}
这是调用该类的第二个文件:
require_once 'student.php';
$collegeStudent = new Student('0422', 'John Smith', '315 Home St', 'Georgia', '30204', '20');
list($id_error, $name_error, $address_error, $state_error, $zip_error, $age_error) = explode(',', $collegeStudent);
print $id_error == 'TRUE' ? 'Student ID update successful<br/>' : 'Student ID update not successful<br/>';
print $name_error == 'TRUE' ? 'Name update successful<br/>' : 'Name update not successful<br/>';
print $address_error == 'TRUE' ? 'Address update successful<br/>' : 'Address update not successful<br/>';
print $state_error == 'TRUE' ? 'State update successful<br/>' : 'State update not successful<br/>';
print $zip_error == 'TRUE' ? 'Zip code update successful<br/>' : 'Zip code update not successful<br/>';
print $age_error == 'TRUE' ? 'Age update successful<br/>' : 'Age update not successful<br/>';
// ------------------------------ Set Properties--------------------------------
print '------------------------------------------------------------------------<br/>';
$student_error_message = $collegeStudent->set_student_id(0422);
print $student_error_message == true ? 'Student ID update successful<br />' : 'Student ID update not successful<br />';
$student_error_message = $collegeStudent->set_student_name('John Smith');
print $student_error_message == true ? 'Name update successful<br />' : 'Name update not successful<br/>';
$student_error_message = $collegeStudent->set_student_address('315 Home St');
print $student_error_message == true ? 'Address update successful<br />' : 'Address update not successful<br/>';
$student_error_message = $collegeStudent->set_student_state('Georgia');
print $student_error_message == true ? 'State update successful<br />' : 'State update not successful<br/>';
$student_error_message = $collegeStudent->set_student_zip(30204);
print $student_error_message == true ? 'Zip code update successful<br />' : 'Zip code update not successful<br/>';
$student_error_message = $collegeStudent->set_student_age(20);
print $student_error_message == true ? 'Age update successful<br />' : 'Age update not successful<br/>';
print '------------------------------------------------------------------------<br/>';
// ------------------------------ Get Properties--------------------------------
print $collegeStudent->get_student_id() . '<br />';
print $collegeStudent->get_student_name() . '<br />';
print $collegeStudent->get_student_address() . '<br />';
print $collegeStudent->get_student_state() . '<br />';
print $collegeStudent->get_student_zip() . '<br />';
print $collegeStudent->get_student_age() . '<br />';
$student_properties = $collegeStudent->get_properties();
list($student_id, $student_name, $student_address, $student_state,
$student_zip, $student_age) = explode(',', $student_properties);
print "Student ID is $student_id. Student name is $student_name. Student address
is $student_address. Student state is $student_state. Student zip is $student_zip.
Student age is $student_age.";
以下是我的输出:
答案 0 :(得分:0)
两部分问题,两部分答案:
要修复名称验证,您可以先使用preg_replace
删除空白,然后检查该值是否仅包含字母字符。但要注意,任何带撇号的名称(例如O&#39; Donne)都不会验证。
function set_student_name($value)
{
$error_message = true;
(ctype_alpha(preg_replace('/\s+/', '', $value)) && strlen($value) <= 20) ? $this->student_name = $value : $error_message = false;
return $error_message;
}
其次,你看到274是因为你把这个数字写成了八进制数。 PHP识别任何文字编号,前导0为八进制(表示基数为8)。如果您需要将学生ID存储为前导0,则可以将其存储为字符串,或者您可以使用getter函数将其添加为0。
public function get_student_id()
{
return str_pad($this->student_id, 4, "0",STR_PAD_LEFT);
}