Pain Posted October 16, 2012 Share Posted October 16, 2012 (edited) 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 October 16, 2012 by Pain Quote Link to comment https://forums.phpfreaks.com/topic/269527-cant-use-method-return-value-in-write-context/ Share on other sites More sharing options...
Barand Posted October 16, 2012 Share Posted October 16, 2012 You are trying to allocate a value to the method result check out the difference between = and == Quote Link to comment https://forums.phpfreaks.com/topic/269527-cant-use-method-return-value-in-write-context/#findComment-1385501 Share on other sites More sharing options...
Pain Posted October 16, 2012 Author Share Posted October 16, 2012 (edited) 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 October 16, 2012 by Pain Quote Link to comment https://forums.phpfreaks.com/topic/269527-cant-use-method-return-value-in-write-context/#findComment-1385503 Share on other sites More sharing options...
TOA Posted October 16, 2012 Share Posted October 16, 2012 (edited) 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 October 16, 2012 by TOA Quote Link to comment https://forums.phpfreaks.com/topic/269527-cant-use-method-return-value-in-write-context/#findComment-1385504 Share on other sites More sharing options...
Pain Posted October 16, 2012 Author Share Posted October 16, 2012 (edited) 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 October 16, 2012 by Pain Quote Link to comment https://forums.phpfreaks.com/topic/269527-cant-use-method-return-value-in-write-context/#findComment-1385646 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.