1

I have a class like

class bank
{
    public $accounts;
    public function __construct()
    {
           $accounts = new Accounts();
    }

    public function fun1()
    {
           ///some code
    }
}

Inside fun1(), I dont get the auto complete(in PHPStorm and Eclipse) feature when using

$this->accounts->..any function

But it works fine when, directly using

$accounts->..auto complete works fine here

Can we achieve the same in the first case?

UPDATE: Thanks to Berry Langerak for rightly pointing it out.

Also, is it possible to

class bank
{
    public $accounts;
    public function __construct()
    {
           $this->accounts = new Accounts();
    }

    public function fun1()
    {
           ///Note changing the reference now
           $this->accounts = new OldAccounts();
           $this->accounts->..it still shows the functions of Accounts Class, can we override this setting in PHPStorm
    }
}

Can we override the behavior and show the functions of the new class, the reference is pointing to

4

3 回答 3

3

That's because you're setting "new Accounts" to a local variable in your constructor, versus setting it to the class variable (no $this);

class bank
{
    public $accounts;
    public function __construct()
    {
           $this->accounts = new Accounts();
    }

    public function fun1()
    {
           $this->accounts->doStuff( );

           /* @var OldAccounts $this->accounts */
           $this->accounts = new OldAccounts;
    }
}
于 2012-08-29T14:36:20.147 回答
2

It's because the variable has not been documented. This will let the IDE know about the reference (line #3):

/* @var Accounts */
$accounts
于 2012-08-29T14:30:21.193 回答
0

I think it's because you haven't defined it as a class variable. IE it should be protected $accounts, public $accounts or private $accounts

于 2012-08-29T14:33:27.143 回答