Bug 373414 - Accessing $this as an array marks the code as syntax error, even though it is a correct syntax in PHP
Summary: Accessing $this as an array marks the code as syntax error, even though it is...
Status: RESOLVED DUPLICATE of bug 346876
Alias: None
Product: kdevelop
Classification: Applications
Component: Language Support: PHP (show other bugs)
Version: 5.0.3
Platform: Arch Linux Linux
: NOR normal
Target Milestone: ---
Assignee: kdevelop-bugs-null
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-12-08 08:15 UTC by Jan Pavlicek
Modified: 2017-02-28 08:45 UTC (History)
1 user (show)

See Also:
Latest Commit:
Version Fixed In:
Sentry Crash Report:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jan Pavlicek 2016-12-08 08:15:10 UTC
Using this construction, which is perfectly valid in PHP:

<?php

class A implements \ArrayAccess
{

    public $values = [];


    public function setTest()
    {
        $this['test'] = 'test';
    }



    public function offsetGet($offset)
    {
        return $this->values[$offset];
    }



    public function offsetSet($offset, $value)
    {
        $this->values[$offset] = $value;
    }



    public function offsetExists($offset)
    {
        return array_key_exists($offset, $this->values);
    }



    public function offsetUnset($offset)
    {
        unset($this->values[$offset]);
    }
}


$test = new A;
$test->setTest();
echo $test['test'];


there will be a red background for the line $this['test'] = 'test', saying "Cannot re-assign $this". But that is in fact possible due to $this being an instance of class implementing the ArrayAccess interface, thus allowing it to be accessed as array.

Please look in to this, many frameworks use this kind of access heavily for forms, components etc. and it is very distracting.

Thanks and have a nice day!
Comment 1 Alexander Zhigalin 2017-02-28 08:45:02 UTC

*** This bug has been marked as a duplicate of bug 346876 ***