Ali
Ali

Reputation: 22317

PHP performance and memory when using ORM

I'm using PDO to fetch rows to object. I have two question about that:

1- Using private properties in object and returning them using __get() method which may call another function itself for example getLabel(), how this affect php performance when retrieving a lot of records? Is it acceptable?

an example:

class model
{
  private p1;
  public p2;
  function __get($name)
  {
    return $this->{'get'.$name}();
  }
  function getP1()
  {
    return '';
  }
}

$obj = new model;
$obj->p1 VS. $obj->p2

2- For class with many columns, defining all those columns in the class definition, then selecting some of them when retrieving a lot of records, how this affect php memory usage as they are defined in objects with null values? is it acceptable?

I don't fetch the rows one by one and not all at once.

an example:

class model
{
  public $p1 = null;
  public $p2 = null;
  public $p3 = null;
  public $p4 = null;
  public $p5 = null;
  public $p6 = null;
  public $p7 = null;
  public $p8 = null;
  public $p9 = null;
  public $p10 = null;
}

sql: select p1, p2 from model limit 100

each object has 8 unused property. how this affect php performance and memory?

As much as I know doctrine use these two patterns. how do they overcome this affecting php performance?

Upvotes: 0

Views: 136

Answers (1)

Mike B
Mike B

Reputation: 32145

Using private properties in object and returning them using __get() method which may call another function itself for example getLabel(), how this affect php performance when retrieving a lot of records? Is it acceptable?

Magic methods are expensive compared to other common workarounds. Exactly how expensive is hard to say without seeing your application.

__get/__set/__call performance questions with PHP

For class with many columns, defining all those columns in the class definition, then selecting some of them when retrieving a lot of records, how this affect php memory usage as they are defined in objects with null values? is it acceptable?

As long as you iterate over the result-set using PDO your memory shouldn't be too high. However, if you iterate over the results and save them into a collection then your entire result-set will be hogging a lot of memory. As always, you want to select the LEAST amount of fields and rows possible.

Upvotes: 1

Related Questions