3

I'm using codeigniter and trying to make an if statement for if the controllers have changed. What I have right now gives the error.

if (isset($this->session->userdata('lastUrl')) && $this->session->userdata('lastUrl') != $this->router->class) {
     echo 'new controller';
}

$this->session->set_userdata('lastUrl', $this->router->class);

This code is in the constructor of the controller so it will be run on every page.

Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202
Dylan Buth
  • 1,648
  • 5
  • 35
  • 57
  • Duplicate of http://stackoverflow.com/questions/1075534/cant-use-method-return-value-in-write-context but out of close votes. – Toon Krijthe Oct 05 '12 at 21:58

2 Answers2

3

you can check this by method_exists() function

if (method_exists($this->session->userdata('lastUrl')) && $this->session->userdata('lastUrl') != $this->router->class) {
     echo 'new controller';
}
NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
2

isset is a special language construct that can only be used on variables or array keys, not methods. This should have the same functionality:

$this->session->userdata('lastUrl') !== null && ...
Explosion Pills
  • 188,624
  • 52
  • 326
  • 405