Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php - reset/clear an object?

Tags:

What I'm trying to do is use a temporary object to store values and then reset it back to empty without having to uset($tmpObject); ?

Here is some example code:

class Object {     function ResetObject(){         // code to remove all variables in an object here?     } }  $tmpObject = new Object();  foreach ($myArray as $arr){     $tmpObject->val1 = "string1";     $tmpObject->val2 = "string2";     $tmpObject->val3 = "string3";     $tmpObject->val4 = "string4";            $template->set('tmpObject',$tmpObject);     $tmpObject->ResetObject(); } 

Anyone have any ideas?

like image 216
Joe Avatar asked Oct 02 '10 09:10

Joe


People also ask

How to reset Object in PHP?

PHP reset() Function"<br>"; echo next($people) . "<br>"; echo reset($people);

What is reset() in PHP?

The reset() function sets the internal pointer of an array to its first element. It returns the value of the first array element. It returns FALSE if the array is empty.

What is reset function?

The Reset function resets a control to its Default property value. Any user changes are discarded. You cannot reset controls that are within a Gallery or Edit form control from outside those controls. You can reset controls from formulas on controls within the same gallery or form.


1 Answers

class Object {     function ResetObject() {         foreach ($this as $key => $value) {             unset($this->$key);         }     } } 

See: Object iteration

like image 68
NikiC Avatar answered Oct 02 '22 17:10

NikiC