Darryl Hein
Darryl Hein

Reputation: 145097

Symfony2 – Is there a standard for the name of entity var inside a controller & view?

I'm curious if there's a standard/recommended practice for the name of the variable storing the entity in a controller action or view.

public function demoAction(User $entity)
{
    return $this->render('...', [
        'entity' => $entity,
    ]);
}

I've seen some places where it's done this way (always name the main entity var $entity) and other's where $entity is $user (for example).

In my opinion, using $entity everywhere get's confusing when there are 2 entities and then you either have one called "entity" and the other something else.

Is there a standard/recommended practice? For Symfony? Or maybe something for the wider PHP world? Or for an ORM, ie, Doctrine?

Upvotes: 0

Views: 117

Answers (2)

mblaettermann
mblaettermann

Reputation: 1943

Yeah, call them what they are, like $user, $userProfile ...

The $entity is from autogenerated stuff Symfony creates. Maybe this behavior could be optimized in Symfony3, so that the generated variable names are derived from the concrete class name.

Upvotes: 3

chalasr
chalasr

Reputation: 13167

Unlike the methods naming conventions (e.g. yourmethodAction in controllers which the routing process depends on), you can name your variables using your own appreciation.

Of course, as in all other programming languages and frameworks, choose logical and comprehensible names.

For entities, I generally use the name of the entity, in camelcase.

i.e. For an entity named ProductCategory, you can use $productCategory

See the Symfony coding standards for more informations.

Upvotes: 1

Related Questions