I have this class Zgh\FEBundle\Entity\User
which extends FOS\UserBundle\Model\User
.
use FOS\UserBundle\Model\User as BaseUser;
class User extends BaseUser implements ParticipantInterface
{
use BasicInfo;
// ..
}
And BaseUser
class:
abstract class User implements UserInterface, GroupableInterface
{
protected $id;
// ..
}
And BaseInfo
trait:
trait BasicInfo
{
/**
* @ORM\Column(type="string", length=255)
* @ORM\Id
* @ORM\GeneratedValue(strategy="NONE")
*/
protected $id;
// ..
}
But when I run my code i get this error:
Strict standards: FOS\UserBundle\Model\User and Zgh\FEBundle\Model\Partial\BasicInfo define the same property ($id) in the composition of Zgh\FEBundle\Entity\User. This might be incompatible, consider using accessor methods in traits instead.
I'm using Symfony framework.
Is there anyway to resolve this conflict between the trait and the parent class object about this property ?
What is a trait ? In PHP, a trait is a way to enable developers to reuse methods of independent classes that exist in different inheritance hierarchies. Simply put, traits allow you to create desirable methods in a class setting, using the trait keyword. You can then inherit this class through the use keyword.
An interface is a contract that says “this object is able to do this thing”, whereas a trait is giving the object the ability to do the thing. A trait is essentially a way to “copy and paste” code between classes.
OOP traits solve this problem. Traits are used to declare methods that can be used in multiple classes. Traits can have methods and abstract methods that can be used in multiple classes, and the methods can have any access modifier (public, private, or protected).
Only classes can be extended. Traits are not classes.
No, it's not yet possible to rewrite a mapped property by using a Trait.
Also, a possible alternative is to use multiple abstract entity classes, and extend your child entities depending on your need.
i.e.
<?php
use FOS\UserBundle\Model\User as BaseUser;
abstract class AbstractStrategyNoneEntity extends BaseUser
{
/**
* @ORM\Column(type="string", length=255)
* @ORM\Id
* @ORM\GeneratedValue(strategy="NONE")
*/
protected $id;
}
abstract class AbstractStrategyAutoEntity extends BaseUser
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*
*/
protected $id;
}
And extend one of them in your child entities.
/**
* @ORM\Entity
*/
class Child extends AbstractStrategyNoneEntity
{
// Inherited mapping
}
Hopes this answers your question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With