Skip to content

PHP function to reset instance variables

Quick snippet to reset all public instance variables inside a PHP class back to their original values.

public function reset() {
        foreach (get_class_vars(get_class($this)) as $name => $default) {
            $this->$name = $default;
        }
    }

Can’t take credit for the code though. Found it on a website somewhere and have used it in pretty much every class I build. Problem is I can’t remember where it was originally.

Published inProgramming

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *