php - preg_match not accepting $this -
am being stupid or preg_match()
not accept global class variables?
class test{ private$username,$usernamevalidation; public function __construct($username){ $this->username=$username; $this->usernamevalidation="/^[a-za-z0-9]{0,8}$/"; } public function validate(){ if(!preg_match($this->usernamevalidation,$this->username)){ //failed; } } }
every time compile function this, seems tell me the expression empty. seems work preg_match("/^[a-za-z0-9]{0,8}$/",$this->username);
.
you can try keeping regex assignment @ class level:
class test { const usernamevalidation = '/^[a-za-z0-9]{0,8}$/'; private $username; public function __construct($username){ $this->username=$username; } public function validate(){ if(!preg_match(usernamevalidation, $this->username)) { //failed; } } }
Comments
Post a Comment