Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Traits - property conflict with parent class

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 ?

like image 610
Rafael Adel Avatar asked Jun 11 '14 08:06

Rafael Adel


People also ask

What is a trait class?

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.

What is the difference between trait and interface?

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.

What are the key oop traits?

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).

Can a trait extend a class PHP?

Only classes can be extended. Traits are not classes.


1 Answers

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.

like image 131
chalasr Avatar answered Oct 05 '22 20:10

chalasr