PHP override get set methods -


is possible override , set methods parameters have set? every time set param have access work it/ validate it?

class user {     public $username;     function custom_set($name, $value) {       if(strlen($value) < 5) {         return "not long enough";       } else {         $this->$name = $value;       }     }   }  $u = new user(); $u->username = "ted"; echo $u->username; 

outputs: "not long enough"

this simplified example wrote out , contains errors, it's convey trying do.

basically everytime call $u->username = "anything"; want custom_set method called.

i don't want validation in constructor , don't want create separate methods $u->setval("ted");

is possible?

make attributes protected or private __get , __set called , can delegate whatever logic want.

personally, have yet see case consider using public property anything.


Comments

Popular posts from this blog

Add email recipient to all new Trac tickets -

400 Bad Request on Apache/PHP AddHandler wrapper -

php - Change action and image src url's with jQuery -