Symfony call get by Name from variable

Symfony offers a special PropertyAccessor you could use:

use Symfony\Component\PropertyAccess\PropertyAccess;

$accessor = PropertyAccess::createPropertyAccessor();

class Person
{
    private $firstName = 'Wouter';

    public function getFirstName()
    {
        return $this->firstName;
    }
}

$person = new Person();

var_dump($accessor->getValue($person, 'first_name')); // 'Wouter'

http://symfony.com/doc/current/components/property_access/introduction.html#using-getters


You can do it like this :

// For example, to get getId()
$reflectionMethod = new ReflectionMethod('AppBundle\Entity\YourEntity','get'.$soft[0]);
$i[] = $reflectionMethod->invoke($yourObject);

With $yourObject being the object of which you want to get the id from.

EDIT : Don't forget the use to add :

use ReflectionMethod;

Hope this helps.

Tags:

Getter

Symfony