phpsa icon indicating copy to clipboard operation
phpsa copied to clipboard

[Analyzer] Unexpected use of $this

Open ovr opened this issue 8 years ago • 4 comments

refs https://wiki.php.net/rfc/this_var

// Запретить использование в качестве имени параметра
function foo($this) {
}

// Запретить использование в качестве статической переменной
static $this; 

// Запретить использование как глобальной переменной
global $this; 

// Запретить использование в качестве переменной исключения в catch
try {
  ...
} catch (Exception $this) { // Fatal error: Cannot re-assign $this
}

// Запретить использование в foreach
foreach ($a as $this) { 
}

// Запретить использование в unset()
unset($this); 

// Запретить обращение через $$
$a = "this";
$$a = 42; 

ovr avatar May 30 '16 09:05 ovr

ping @ddmler What do you think about it?

ovr avatar May 30 '16 11:05 ovr

I will try to implement it

ddmler avatar May 30 '16 11:05 ddmler

  • [ ] setting $this via variable variables
  • [x] $this function parameter
  • [ ] $this via parse_str(), extract(), list() (do we really need that?)
  • [ ] and this: (?)
class C {
  function foo(){
    $a =& $this;
    $a = 42;
    var_dump($this); // prints object(C)#1 (0) {}, php-7.0 printed int(42)
  }
}
$x = new C;
$x->foo();

ddmler avatar Sep 06 '16 18:09 ddmler

php -a
Interactive mode enabled

php > class A {function foo() {$a =& $this; $a = 42; echo phpversion()."\n"; var_dump($this);}}
php > $x = new A;
php > $x->foo();

5.6.25-2+deb.sury.org~trusty+1
int(42)
php -a
Interactive mode enabled

php > class A {function foo() {$a =& $this; $a = 42; echo phpversion()."\n"; var_dump($this);}}
php > $x = new A;
php > $x->foo();

7.0.10-2+deb.sury.org~trusty+1
int(42)

sergeyklay avatar Sep 06 '16 18:09 sergeyklay