简体   繁体   中英

How to override/overload setters of BaseUser (from FosUserBundle)?

I am using Symfony 2.0.

I have an Employee class which extends from BaseUser

I need to override setUsername method (among others) :

My method is Employee.php is like that :

public function setUsername()
{
    $name=$this->getName();

    $this->username=($name);//just an example
}

But when I clear the cache I have the error :

MyBundle\Entity\Employee::setUsername() must be compatible with that of FOS\UserBundle\Model\UserInterface::setUsername()

EDIT :
I have seen that the method setUsername in FOS\\UserBundle\\Model\\UserInterface need the $username argument, but my setUsername function as it is defined doesn't need this argument. More generally, my question is how to redefine/override methods and also how to add his own constraints to fields and methods defined in Fos User. Thanks a lot

How can I override this function? I tried parent::setUsername() but I have a parse error:

PHP Parse error:  syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM, expecting '('

The function is defined as:

function setUsername($username);

So your function must be the same but yours is missing the parameter. Add it and you should be fine.

This actually has nothing to do with Symfony2 or the FOS Bundle in specific, that is how PHP generally works. So try to understand the error message more properly. And next time you see an error message you do not fully understand, ask specifically about the error message and the part that is not clear to you. Just saying: ask directly about the part you want to know about. Not about the part you try to do (there is nothing wrong with trying, just saying, we all do learning by doing. but the key point is to ask the right questions ;))

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM