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>

Edited by Pain
Link to comment
Share on other sites

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!

Edited by Pain
Link to comment
Share on other sites

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();
}
}

Edited by TOA
Link to comment
Share on other sites

Thank you, everything works just fine:)!

 

However i am positive that the structure is not very well made. Can you give me any advice on how could I improve the structure of this code?

Edited by Pain
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.