Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why to use getter setters for doctrine2 classes

Why we generally use getter setters for doctrine2 classes to save and get data?

like below:

<?php
// src/Product.php
class Product
{
    /**
     * @var int
     */
    protected $id;
    /**
     * @var string
     */
    protected $name;

    public function getId()
    {
        return $this->id;
    }

    public function getName()
    {
        return $this->name;
    }

    public function setName($name)
    {
        $this->name = $name;
    }
}

If we make the the class properties public we can save the data without getter setters. Isn't it?

<?php
// src/Product.php
class Product
{
    /**
     * @var int
     */
    public $id;
    /**
     * @var string
     */
    public $name;
}
like image 756
Jaskaran Singh Avatar asked Dec 25 '22 17:12

Jaskaran Singh


1 Answers

  1. You can't create proxy calls on properties which are required for lazy loaded elements. That is quite heavly used tool by Doctrine.
  2. Getters and setters gives you more flexibility in general than properties.
  3. If overhead of getters and setters is a true issue you should probably consider switching from PHP to some "faster-in-general" platform.
like image 86
Crozin Avatar answered Jan 04 '23 18:01

Crozin