Reputation: 6974
Is it "better" to instantiate objects as needed or right in the beginning of the code? Note in the examples one may not need the functionality of obj2.
obj1 = new Object1();
obj2 = new Object2();
obj1->run;
//lots of code
if (condition) {
obj2->doThis();
}
OR
obj1 = new Object1();
obj1->run;
//lots of code
if (condition) {
obj2 = new Object2();
obj2->doThis();
}
Upvotes: 3
Views: 138
Reputation: 57650
Just 2 rules
Unnecessary object creation consumes more memory and makes program run slower. For small projects it'll not make any significant performance difference. But for big projects or long time running Cron it becomes performance issue.
Upvotes: 3
Reputation:
It doesn't particularly matter where in the global scope you instantiate an object if you actually need it. Just don't go instantiating objects you won't use.
However, if you want to have the ability to successfully unit test your code you should avoid using the new
keyword to instantiate objects inside other objects. You should avoid doing things like:
class MyClass
{
public function myMethod()
{
$dependency = new DependencyClass; // <--- very bad for testability
echo $dependency->doSomething();
}
}
class DependencyClass
{
public function doSomething()
{
return 'did something';
}
}
Instead, your classes should ask for their dependencies in the constructor method signature, not look for them elsewhere in the code. The better way to do the above would be:
class MyClass
{
protected $dependency;
public function __construct(DependencyClass $dep)
{
$this->dependency = $dep;
}
public function myMethod()
{
echo $this->dependency->doSomething();
}
}
$myClass = new MyClass(new DependencyClass); // <-- inject the dependency
Upvotes: 1
Reputation: 270637
For readability, I would tend to instantiate closest to when the object is going to be used. I find it easier to remember what $obj2
is when I see:
if (condition) {
$obj2 = new Object2();
$obj2->doThis();
}
... than if I see
if (condition) {
// $obj2 created 1000lines ago
// and I forgot all about it
$obj2->doThis();
}
PHP will manage memory by freeing objects no longer in use. So if you have instantiated new Object1()
and it is no longer needed before new Object2()
is instantiated, you will have fewer objects in memory. But if you are not experiencing performance problems, this should not be a big concern.
If you were to instantiate lots and lots of objects before actually needing them, monitory your memory consumption and think twice about it...
// Need these right away...
for ($i=0; $i<10000000; $i++) {
$obj1s[] = new Object1();
}
// Don't actually need these until the end of the script
// but built ten million of them anyway...
for ($i=0; $i<10000000; $i++) {
$obj2s[] = new Object2();
}
Upvotes: 1
Reputation: 76736
I prefer to declare the variables at the top (of the function / file), but only instantiate the objects when needed.
$ob1;
$obj2;
// some code...
$obj1 = new Object1();
$obj1->run;
// lots of code
if (condition) {
$obj2 = new Object2();
$obj2->doThis();
}
This allows you to "have your cake and eat it too." Anyone reading your code knows what to expect, because all variables are declared at the start, but you don't waste memory by needlessly instantiating objects.
Upvotes: 0
Reputation: 98489
Depends: is your time inside inside the if statement more important than time spent at the start of the application? Do you need the object more than once?
If the answer to both questions is no, instantiate the object when you need it.
Upvotes: 1