Jump to content

instance object best practice (for optimization)


rwhite35

Recommended Posts

Q:  Should instancing an object be held off until needed - or - instanced early and held in memory until (it may/or may not) be called?

 

Here is an example of the type situation where an custom error reporting class is instanced only when some test has failed.  The error reporting is for authorized users running an application from protected sub sections(RESTish solution).

/*
* check form input and $_FILES upload
* if test fails, instance errorwrapper::factory object and call errorReport method
* testfilesize function, compares file size limit, set pass1 to true on success
* testfiletype function, compares file to allowed MIME types, set pass2 to true on success
* svalidate returns array $filteres, sanitized input on success, error message(s) on fail
* param boolean $pass[1,2], set initial test condition 
* param int MAX_FILE_SIZE, constant defines max file size allowed.
*/
$pass1 = false;
$pass2 = false;
if(isset($_FILES)) {
  $ftype = $_FILES['img_file']['type'];
  $fsize = $_FILES['img_file']['size'];
  testfilesize($pass1,MAX_FILE_SIZE,$fsize);
  testfiletype($pass2,$ftype);
}
if($pass1 == false) {
  $erparams=array('cat'=>3,'src'=>"addform_control",'ref'=>"addform",'fault'=>"over file size limit");
  $errObj=errorwrapper::factory($erparams);
  $errObj->errorReport();
  exit();
} elseif ($pass2 == false) {
  $erparams=array('cat'=>3,'src'=>"addform_control",'ref'=>"addform",'fault'=>"illegal file type");
  $errObj=errorwrapper::factory($erparams);
  $errObj->errorReport();
  exit();
}
/* 
* process form input, all fields checked before returning filteres
* could report multiple errors, mode sets level of sanitization. 
*/
$filteres = svalidate($_POST,$mode=1);
if($filteres['exit']==1) {
  foreach($filteres['err'] as $err) { $mes .= $err."\n"; }
  $erparams=array('cat'=>2,'src'=>"addfo",'ref'=>"list_viewer",'fault'=>$mes);
  $errObj=errorwrapper::factory($erparams);
  $errObj->errorReport();
  exit();
} else {
  //continue processing here, error checking over
}

In this case, the object $errObj is only instanced on a failed condition.  However, $errObj could be instantiated before the conditional test and only calls the method when there is a failed condition.

 

It seems like "six to one half dozen"  but these applications are large and complex, so I'm looking to squeeze out any worthwhile optimization.  

 

Thanks for considering the question. 

It's better to only instance objects when you need them. Each object instance consumes memory and each PHP instance is limited to a certain amount of memory. Keeping memory consumption low means more PHP instances can be spawned and thus more requests can be handled which increases performance.

Ignace, thanks for the reply.  I plan to do some benchmarking (if I can get to it) this week.  I'll post any results here.  This occurs often in large scale projects (over 50K lines of code), but it seems like its more preference than a hard fast rule.  That's my sense of it anyway.  Thanks again, I appreciate your insights.  

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.