调用非对象的成员函数 verifyInput()
Call to a member function verifyInput() on a non-object
这似乎是一个很常见的问题,但我找不到问题的答案。我遇到了可怕的 "Call to a member function verifyInput() on a non-object" 致命错误。
<?php
// empty values
$name = $email = $topic = $message = "";
$nameErr = $emailErr = $topicErr = $messageErr = "";
// link the name tags in HTML
$name = $_POST['name'];
$email = $_POST['email'];
if ($_SERVER["REQUEST_METHOD"] == "POST") {
/* name if */
if (empty($_POST['name'])) {
$nameErr = " * Name is required";
} else {
$name = test_input($_POST["name"]);
}
/* email if */
if ( ! empty($_POST['email'])) {
if ($email->verifyInput($_POST['email'], 6)){
$fill['email'] = $_POST['email']; //$fill is the array of values passed
} else {
$emailErr = " * Email is incorrect - Try Again";
}
} else {
$emailErr = " * Email is required";
}
// Form content
$recipient = "generic@gmail.com";
$subject = "Contact Form";
$formcontent = "4 Days of Fun.ca -
\n\n\nFrom: $name \n\nEmail: $email \n\nSubject: $topic
\n\n\nMessage: $message";
$mailheader = "From: $email - Subject: $topic \r\n";
//function to send mail
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank you for contacting us, $name! \nWe will respond to you soon!";
}
//function for test_input
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
// Validation of Inputs
function verifyInput($input, $type){
if ($type == 0) $pattern = '/^1$/';//just the number 1
elseif ($type == 1) $pattern = '/^[0-9]+$/';//just integers
elseif ($type == 2) $pattern = '/^[A-Za-zÁ-Úá-úàÀÜü]+$/';//just letters
elseif ($type == 3) $pattern = '/^[0-9A-Za-zÁ-Úá-úàÀÜü]+$/';//integers & letters
elseif ($type == 4) $pattern = '/^[A-Za-zÁ-Úá-ú0-9àÀÜü\s()\/\'":\*,.;\-!?&#$@]{1,1500}$/';//text
elseif ($type == 5) $pattern = '/^[A-Za-zÁ-Úá-úàÀÜü0-9\']+[A-Za-zÁ-Úá-úàÀÜü0-9 \'\-\.]+$/';//name
elseif ($type == 6) $pattern = '/^.+@[^\.].*\.[a-z]{2,}$/';//e-mail
elseif ($type == 7) $pattern = '/^((\(0?[1-9][0-9]\))|(0?[1-9][0-9]))?[ -.]?([1-9][0-9]{3})[ -.]?([0-9]{4})$/';//brazilian phone
if (preg_match($pattern, $input) == 1) return true;
else return false;
}
?>
这是错误的地方:if ($email->verifyInput($_POST['email'], 6)){
然而我很困惑......我查了一下,到处都说传入的变量即 $email
可能不是一个对象.但是变量在这里声明为对象: $email = $_POST['email'];
不是吗?还是我错过了一些明显的大事?
我刚开始学习 PHP 但我已经掌握了很多 C++ 的基础知识,所以我已经有了很多相似之处...这不是其中之一。
也许我只是不理解 ->
运算符,或者因为其他原因它不是对象?
提前致谢。
$email->verifyInput()
将调用 class email
中包含的方法(函数)verifyInput()
,但在这里我可以看到您的函数不是成员任何class,所以如果你需要调用这个函数,你只需要改变:
if ($email->verifyInput($_POST['email'], 6)){...
至:
if (verifyInput($_POST['email'], 6)){...
关于你关于 ->
的问题:
正如我上面提到的 ->
在实例化后使用 class 后跟成员或方法名称。可能你需要阅读更多关于 PHP 的内容,我建议 this course, or you can jump directly to get a course about PHP OOP right here.
这似乎是一个很常见的问题,但我找不到问题的答案。我遇到了可怕的 "Call to a member function verifyInput() on a non-object" 致命错误。
<?php
// empty values
$name = $email = $topic = $message = "";
$nameErr = $emailErr = $topicErr = $messageErr = "";
// link the name tags in HTML
$name = $_POST['name'];
$email = $_POST['email'];
if ($_SERVER["REQUEST_METHOD"] == "POST") {
/* name if */
if (empty($_POST['name'])) {
$nameErr = " * Name is required";
} else {
$name = test_input($_POST["name"]);
}
/* email if */
if ( ! empty($_POST['email'])) {
if ($email->verifyInput($_POST['email'], 6)){
$fill['email'] = $_POST['email']; //$fill is the array of values passed
} else {
$emailErr = " * Email is incorrect - Try Again";
}
} else {
$emailErr = " * Email is required";
}
// Form content
$recipient = "generic@gmail.com";
$subject = "Contact Form";
$formcontent = "4 Days of Fun.ca -
\n\n\nFrom: $name \n\nEmail: $email \n\nSubject: $topic
\n\n\nMessage: $message";
$mailheader = "From: $email - Subject: $topic \r\n";
//function to send mail
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank you for contacting us, $name! \nWe will respond to you soon!";
}
//function for test_input
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
// Validation of Inputs
function verifyInput($input, $type){
if ($type == 0) $pattern = '/^1$/';//just the number 1
elseif ($type == 1) $pattern = '/^[0-9]+$/';//just integers
elseif ($type == 2) $pattern = '/^[A-Za-zÁ-Úá-úàÀÜü]+$/';//just letters
elseif ($type == 3) $pattern = '/^[0-9A-Za-zÁ-Úá-úàÀÜü]+$/';//integers & letters
elseif ($type == 4) $pattern = '/^[A-Za-zÁ-Úá-ú0-9àÀÜü\s()\/\'":\*,.;\-!?&#$@]{1,1500}$/';//text
elseif ($type == 5) $pattern = '/^[A-Za-zÁ-Úá-úàÀÜü0-9\']+[A-Za-zÁ-Úá-úàÀÜü0-9 \'\-\.]+$/';//name
elseif ($type == 6) $pattern = '/^.+@[^\.].*\.[a-z]{2,}$/';//e-mail
elseif ($type == 7) $pattern = '/^((\(0?[1-9][0-9]\))|(0?[1-9][0-9]))?[ -.]?([1-9][0-9]{3})[ -.]?([0-9]{4})$/';//brazilian phone
if (preg_match($pattern, $input) == 1) return true;
else return false;
}
?>
这是错误的地方:if ($email->verifyInput($_POST['email'], 6)){
然而我很困惑......我查了一下,到处都说传入的变量即 $email
可能不是一个对象.但是变量在这里声明为对象: $email = $_POST['email'];
不是吗?还是我错过了一些明显的大事?
我刚开始学习 PHP 但我已经掌握了很多 C++ 的基础知识,所以我已经有了很多相似之处...这不是其中之一。
也许我只是不理解 ->
运算符,或者因为其他原因它不是对象?
提前致谢。
$email->verifyInput()
将调用 class email
中包含的方法(函数)verifyInput()
,但在这里我可以看到您的函数不是成员任何class,所以如果你需要调用这个函数,你只需要改变:
if ($email->verifyInput($_POST['email'], 6)){...
至:
if (verifyInput($_POST['email'], 6)){...
关于你关于 ->
的问题:
正如我上面提到的 ->
在实例化后使用 class 后跟成员或方法名称。可能你需要阅读更多关于 PHP 的内容,我建议 this course, or you can jump directly to get a course about PHP OOP right here.