Jump to content

Can't Use Method Return Value In Write Context


Pain

Recommended Posts

Hello. I am learning some php OOP. While building a basic verification class i've faced this error:

 

Fatal error: Can't use method return value in write context in /home/searchqu/public_html/ww3/process_registration.php on line 45

 

Where did i go wrong?

 

Here is my code. Any advice would be greatly appreciated.

 

Thank you.

 

<?php
ob_start();
$submit = $_POST['submit'];
class Login {

function validateEmail($email) {
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
return $email = FALSE;
}else{
return $email = TRUE;
}
}

function validatePassword($password) {
if(strlen($password) < 5) {
return $password = FALSE;
}
else
{
return $password = TRUE;
}
}

function validatePasswordRepeat($password_repeat) {
if ($_POST['password'] !== $password_repeat) {
return $password_repeat = FALSE;
}else{
return $password_repeat = TRUE;
}
}

public function successfullyRegistered() {
header('Location: successfully_registered.php');
}
}

$validation = new Login;
if (isset($submit)) {
$validation->validateEmail($_POST['email']);
$validation->validatePassword($_POST['password']);
$validation->validatePasswordRepeat($_POST['password_repeat']);
if (($validate->validateEmail($_POST['email']) = TRUE) && ($validate->validateEmail($_POST['password']) = TRUE) && ($validate->validateEmail($_POST['password_repeat']) = TRUE)) {
$validation->successfullyRegistered();
)
}

ob_end_flush();

?>

<html>
<head><title>New user</title></head>
<body>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p>Password</p>
<input type="password" name="password" />
<p>Repeat password</p>
<input type="password" name="password_repeat" />
<p>Email</p>
<input type="text" name="email" />
<input type="submit" name="submit" />
</form>
</body>
</html>

I've fixed it a bit:

 

<?php
$submit = $_POST['submit'];
class Login {

function validateEmail($email) {
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
return $email == FALSE;
}else{
return $email == TRUE;
}
}
function validatePassword($password) {
if(strlen($password) < 5) {
return $password == FALSE;
}
else
{
return $password == TRUE;
}
}
function validatePasswordRepeat($password_repeat) {
if ($_POST['password'] !== $password_repeat) {
return $password_repeat == FALSE;
}else{
return $password_repeat == TRUE;
}
}
public function successfullyRegistered() {
header('Location: successfully_registered.php');
}
}
$validation = new Login;
if (isset($submit)) {
$validation->validateEmail($_POST['email']);
$validation->validatePassword($_POST['password']);
$validation->validatePasswordRepeat($_POST['password_repeat']);
if (($validate->validateEmail($_POST['email']) == TRUE) && ($validate->validatePassword($_POST['password']) == TRUE) && ($validate->validatePasswordRepeat($_POST['password_repeat']) == TRUE)) {
$validation->successfullyRegistered();
}
}
?>
<html>
<head><title>New user</title></head>
<body>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p>Password</p>
<input type="password" name="password" />
<p>Repeat password</p>
<input type="password" name="password_repeat" />
<p>Email</p>
<input type="text" name="email" />
<input type="submit" name="submit" />
</form>
</body>
</html>

 

And i get another error now.

Call to a member function validateEmail() on a non-object in /home/searchqu/public_html/ww3/process_registration.php on line 43

 

 

Thanks for spotting that one!

You use $validation everywhere else. Check the object name; you're using $validate.

 

Also, you don't need to call the three lines before because you call them in your if statement, unless I'm missing something. You don't assign the return to anything so you're just wasting calls.

 

Try this

if (isset($submit)) {
if (($validation->validateEmail($_POST['email']) == TRUE) && ($validation->validatePassword($_POST['password']) == TRUE) && ($validation->validatePasswordRepeat($_POST['password_repeat']) == TRUE)) {
$validation->successfullyRegistered();
}
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.