3

Does anyone know how to reset the instance variables via a class method. Something like this:

class someClass 
{
    var $var1 = '';
    var $var2 = TRUE;

    function someMethod() 
    { 
        [...]
        // this method will alter the class variables
    }

    function reset()
    {
        // is it possible to reset all class variables from here?
    }
}

$test = new someClass();
$test->someMethod();
echo $test->var1;

$test->reset();
$test->someMethod();

I know I could simply do $test2 = new SomeClass() BUT I am particularly looking for a way to reset the instance (and its variables) via a method.

Is that possible at all???

1
  • 1
    Welcome to StackOverflow! Great first question. Commented Dec 21, 2009 at 22:08

4 Answers 4

8

You can use reflection to achieve this, for instance using get_class_vars:

foreach (get_class_vars(get_class($this)) as $name => $default) 
  $this -> $name = $default;

This is not entirely robust, it breaks on non-public variables (which get_class_vars does not read) and it will not touch base class variables.

Sign up to request clarification or add additional context in comments.

5 Comments

that's exactly what I was looking for. thanks!! can you explain what you mean by "not touch base class vars"?
Victor Nicollet means the variables that are defined in the parent class. You could declare a class as class B extends A {}; A would be the base, or parent class.
okay. so that means that $test = new A(); $test->reset(); would work, whereas $test = new B(); $test->reset(); wouldnt. Right?
Right. You could replace get_class($this) by a constant 'A' if you were afraid of this happening.
actually this won't apply to my code. but it's good to know! :)
-1

Yes, you could write reset() like:

function reset()
{
    $this->var1 = array();
    $this->var2 = TRUE;
}

You want to be careful because calling new someClass() will get you an entirely new instance of the class completely unrelated to the original.

1 Comment

see above. that's what i am doing right now. but since there are 20+ properties it just seems redundant to do this. i thought maybe there's a php function or some kind of other trick for this...
-1

this could be easy done;

public function reset()
{
    unset($this);
}

4 Comments

I tried it, but it doesn't reset any properties of the object.
then you should try unset(get_class_methods($this));
actualy php resets all object properties if the object is no more needed, try __destruct() put all your cleaning stuff there
since i want to reset them to the initial state unset wouldn't work (unless the initial state would be invoked by some method via the constructor)
-1

Sure, the method itself could assign explicit values to the properties.

public function reset()
{
  $this->someString  = "original";
  $this->someInteger = 0;
}

$this->SetInitialState() from Constructor

Just as another idea, you could have a method that sets the default values itself, and is called from within the constructor. You could then call it at any point later as well.

<?php

  class MyClass {
    private $var;
    function __construct()     { $this->setInitialState(); }
    function setInitialState() { $this->var = "Hello World"; }
    function changeVar($val)   { $this->var = $val; }
    function showVar()         { print $this->var; }
  }

  $myObj = new MyClass();
  $myObj->showVar(); // Show default value
  $myObj->changeVar("New Value"); // Changes value
  $myObj->showVar(); // Shows new value
  $myObj->setInitialState(); // Restores default value
  $myObj->showVar(); // Shows restored value

?>

8 Comments

that's what I am doing right now. but since there's a LOT of properties in the class I thought there might be an easier solution to this...
Frank, what about calling reset() from the Constructor to set the initial state?
wouldn't really make a difference, would it?
Sure it would. The __construct() method would call $this->setInitialState() which sets the object properties to their default state. You can modify them with $this->someMethod(), and restore them with $this->setInitialState().
Frank, you're going to have to have them all written someplace. What I'm suggesting is moving that task into the setInitialState() method, as opposed to immediately within your class.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.