tags:

views:

49

answers:

1

ok, that's a bit odd but here's the situation:

I am working in an MVC context

I have a User class used as a library(User.php)

ANd then I have a controller class that handles input from the user(controller.php)

Here's how it goes:

  1. user registers
  2. registerhandler() is called in controller
  3. registerhandler() calls register() method in User class
  4. register() method creates a User instance with $this and returns a token and echoes a message linking to a verifyhandler() in controller class
  5. verifyhandler calls verify() method in User class
  6. verify() method uses $this to reference the User instance created by register()
  7. boom! $this is not pointing to the same object(created by register() method ) anymore!

I want to maintain that reference yet it seems to take on another value once it is passed to the controller. What can I do about it?

+2  A: 

$this always points to the class instance it is used. So $this in your controller and your library will always point to something different and there is little you can do about it.

What you can do is to change the definition of a register function that it accept parameter you want to work with and reference to this parameter instead of using $this. The other option is to define a field in your class and use that field for reference, this way all class methods can be work on the same object.

I think this is more less you want to achieve.

class UserController {
    // User library instance
    private $User = new User();

    private $token = null;

    public function registerhandler() {
        $this->token = $this->User->register();
    }

    private function verifyhandler() {
        $valid = $this->User->verify($this->token);
        ...
    }
}

class User {
    private $sharedData;
    public function register() {
        // register all common data in $sharedData property
        ...
        return $token;
    }

    public function verify($token) {
        // use shared data and method input to verify the data
        ...
        return true;
    }
}
RaYell
this is the answer I'm looking for...I was spending hours in working with a workaround since I was trying to convert an auth script made without a framework to Kohana and then this propblem came in. Guess I have to use references an recreate the whole class; thanks RaYell!
If this is an acceptable answer to you, click the accept tick thingy.
Ollie Saunders