I am re-learning php after almost 10 years of not using it, and now I want to re-learn it, and correctly this time, but I seem to not be grasping OOP, what am I doing wrong here?
Trying to make a basic login script.
login.php
$act = isset( $_POST['act'] ) ? $_POST['act'] : '' ;
switch ($act) {
case 'login':
$uname = new CleanData($_POST['username']);
$uname-> set_minlength('8');
$uname-> set_maxlength('40');
$uname-> set_pregtype('match');
$uname-> set_regex('/^[\w@._-]{8,40}$/');
$uname-> cleaner();
etc...
class file
class CleanData
{
var $data;
var $minlength;
var $maxlength;
var $pregtype;
var $regex;
function __construct($data)
{
$this->data = $data;
}
function set_minlength($minlength){
$this->$minlength = $minlength;
}
function set_maxlength($maxlength){
$this->$maxlength = $maxlength;
}
function set_pregtype($pregtype){
$this->$pregtype = $pregtype;
}
function set_regex($regex){
$this->$regex = $regex;
}
function cleaner(){
print $this->pregtype."\n";
if ($this->pregtype == 'match') {
print "MATCH REGEX HERE";
}elseif ($this->pregtype == 'replace') {
print "REPLACE REGEX HERE";
}else{
print "UNKNOWN REGEX TYPE";
}
}
}
I get "UNKNOWN REGEX TYPE", which I am sure makes perfect sense, but I dont understand why. Am I even using OOP correctly by writing this class?
Its really hard to re-learn something correctly after doing it wrong, I just want to write it fast the way I know how instead of re-learning everything and writing at a snails pace, but I must stay strong...
I appreciate the help, I always wanted to post here, but I suck at asking for help, always want to do everything myself.
$this->$minlength = $minlength;
^----NO, don't do this
You're trying to set a dynamic attribute based on your argument, which makes no sense at all. eg
set_minlength(42) -> `$this->42 = 42`
set_maxlength(42) -> `$this->42 = 42`
Now you've not only overwritten the minlength value, you have no way of know what the heck this mysterious $this->42
really is. How would get_minlength()
work? $this->????
?
Try
$this->minlength = $minlength;
^---note, no $
and similar for all of your OTHER methods as well.
Try to change these:
$this->minlength = $minlength;
$this->maxlength = $maxlength;
$this->pregtype = $pregtype;
$this->regex = $regex;
Here you are assigning the values to the variables, that are declared above.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.