For each of a bunch of records from my database I instantiate a class. Inside that class I call another class.
Example
$records = array(...);
foreach ($records as $record) {
$a = new A();
$data = $a->doSomething($record);
}
class A {
protected $b;
public function __construct()
{
$this->b = new B();
}
public function doSomething($param)
{
return $this->b->doSomething($param);
}
}
The above code is the way I'm currently doing it. However I was wondering if the below would be better:
$records = array(...);
$b = new B();
foreach ($records as $record) {
$a = new A($b);
$data = $a->doSomething($record);
}
class A {
protected $b;
public function __construct($b)
{
$this->b = $b;
}
public function doSomething($param)
{
return $this->b->doSomething($param);
}
}
What I would like to know, is if this is efficient at all, which of those options is better and if there are any other solutions that are even better.
B
; the latter creates manyB
s. Without knowing what is really going on, we can't advise you properly. See How to Ask. – 200_success Mar 4 at 12:17B::doSomething
is stateless, there could be problems – Steve Mar 4 at 15:53