tags:

views:

74

answers:

4

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???

A: 

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

?>
Jonathan Sampson
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
Frank, what about calling `reset()` from the Constructor to set the initial state?
Jonathan Sampson
wouldn't really make a difference, would it?
Frank
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()`.
Jonathan Sampson
sure. but it doesn't help rewriting all the vars in the setInititalState() method
Frank
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.
Jonathan Sampson
that's what i have right now. but since i need to declare all these vars anyway I wanted to prevent writing all of these vars in the reset (or init) once again. i really like the get_class_vars() method victor suggested. that's more of what I was looking for. but thanks so much for your detailed help!
Frank
Frank, you move all of your declared values into the method, so they aren't redundant.
Jonathan Sampson
A: 

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.

Rob Lund
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...
Frank
A: 

this could be easy done;

public function reset()
{
    unset($this);
}
streetparade
tried this. didn't work :(
Frank
how did you tested that?
streetparade
I tried it, but it doesn't reset any properties of the object.
kiamlaluno
then you should try unset(get_class_methods($this));
streetparade
actualy php resets all object properties if the object is no more needed, try __destruct() put all your cleaning stuff there
streetparade
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)
Frank
A: 

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.

Victor Nicollet
that's exactly what I was looking for. thanks!! can you explain what you mean by "not touch base class vars"?
Frank
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.
kiamlaluno
okay. so that means that$test = new A();$test->reset();would work, whereas$test = new B();$test->reset(); wouldnt. Right?
Frank
Right. You could replace `get_class($this)` by a constant `'A'` if you were afraid of this happening.
Victor Nicollet
actually this won't apply to my code. but it's good to know! :)
Frank