I am still a newbie at coding in general. Over the years the amount of programming that I have done in PHP has always been procedural. I am now working on a project for my course, and was hoping to use it as an opportunity to learn OOP since I come across it everywhere.
I am still trying to really get my head around OOP. I understand the basics (I hope).
Here is my very first attempt. I am working on an AJAX based server-side (PHP) form validation. I have tried to write my validation code along OOP principles.
Does this make sense from an OOP perspective?
require_once 'db.php';
class Registree {
/*Some of the properties*/
var $Username;
var $Email;
var $Password;
var $Password_repeat;
/*Function to check that the $ProposedUsername is not already taken*/
function checkUsername($ProposedUsername) {
$this->$username = $ProposedUsername;
/*Only start checking once the user has entered 4 or more characters into the username field*/
if (strlen($ProposedUsername) => 4) {
/*Make a query from the users table based on the $ProposedUsername*/
$result = mysqli_query($dblink, "SELECT * FROM users WHERE `username` = '$ProposedUsername'")
or die(mysqli_error($dblink));
/*If any rows were affected, then this username is taken*/
if (mysqli_affected_rows($dblink)) > 0) {
echo 'Username already taken';
} else {
echo 'Username available';
}
}
}
}
$this->$username
should most likely be $this->username to assign the$username
property. also you have mixed up=>
with>=
in your condition and also your echoing from your method I think you should use return instead. \$\endgroup\$var
is no longer considered deprecated. From the docs: "since PHP 5.1.3 it is no longer deprecated." It instead acts as an alias for "public." Of course, I agree that being more specific in property declarations is appropriate. \$\endgroup\$