PhpStorm Field accessed via magic method

2019-03-17 02:52发布

问题:

i have ignited datatables Library in my codeigniter library folder.

Some Code from Library

class Datatables
{
    /**
     * Global container variables for chained argument results
     *
     */
    protected $ci;
    protected $table;
    protected $distinct;
    protected $group_by;
    protected $select         = array();
    protected $joins          = array();
    protected $columns        = array();
    protected $where          = array();
    protected $filter         = array();
    protected $add_columns    = array();
    protected $edit_columns   = array();
    protected $unset_columns  = array();

    /**
     * Copies an instance of CI
     */
    public function __construct()
    {
        $this->ci =& get_instance();
    }

i then called the library in model

class Common_Model extends MY_Model{

    function __construct(){
        parent::__construct();
        $this->load->library('Datatables.php');
    }

then i tried to call the library functions

function select_fields_joined_DT($data, $PTable, $joins = '', $where = '', $addColumn = '',$unsetColumn='')
{
    /**
     *
     */
    $this->datatables->select($data);
    if ($unsetColumn != '') {
        unset_column($unsetColumn);
    }
        $this->datatables->from($PTable);
    if ($joins != '') {
        foreach ($joins as $k => $v) {
            //$this->datatables->join($v['table'], $v['condition'], $v['type']);
        }
    }

    if ($addColumn != '') {
        $this->datatables->add_column("Actions", $addColumn);
    }

    $result = $this->datatables->generate();
    return $result;
}

and everything works fine, except that the phpstorm show me error

Field Accessed via magic method

i tried to remove this error with document comments but cant figure out how can i do that.. any help will be appreciated.

回答1:

If you want to remove this without document comments you can uncheck Notify about access to a field via magic method which is found in

Project Settings > Inspections > PHP > Undefined > Undefined field



回答2:

As mentioned by LazyOne in the question comments:

You have to declare them via @property in PHPDoc comment that belongs to that class.

/**
 * @property string $bar
 */
class Foo {

    public function __get($name) {
        if ($name == 'bar') {
            return 'bar';
        }
        return NULL;
    }
}

Snippet from Dmitry Dulepov's article "Quick tip: magic methods and PhpStorm".