0

As it states that it's ok to answer my own question and having found some trick to achieve code completion in Eclipse IDE (for PHP) I would like to share this with other users.

I found some solution here but somehow I can't make doxygen work properly with such declared properties, I found some workaround.

The problem

If you use magic methods, especially for property overloading __get(), __set(), (probably it concerns also __call() and __invoke()), there is a problem, that property names defined in somehow dynamic (as variable variables) and not visible in any scope. It is possible because their names are passed as strings and can be manipulated in any way.

So it is not possible to have those magically accessed properties (or methods) in code completion, and in my case (don't know why, but it doesn't matter), in the generated documentation of the class.

(The code-completion is when the editor helps the programmer by showing all possible properties and methods, thus letting avoid some errors.)

Consider we have the following class:

/**
 * Test class
 */
class myClass {
  private $field_someValue1;
  private $field_someValue2;

  /**
   * Magic method getter for hidden properties.
   * @param string $name
   * @return mixed
   */
  public function __get($name){
    switch($name){
      case 'foo': return $this->$field_someValue1; break;
      case 'bar': return $this->$field_someValue2; break;
      default:
        // handle non-existing property error here
    }
  }

  /**
   * Simply do nothing, but be some public part of the class.
   * @return void
   */
  public function doNothing(){
  }
}

$a = new myClass();

So in my code when I type $a-> the editor should help me by hinting that there are two properties for my object: foo and bar. Of course it can't show $field_someValue1 and $field_someValue2 as they are private.

How to make Eclipse help me?

Community
  • 1
  • 1
Voitcus
  • 4,463
  • 4
  • 24
  • 40

2 Answers2

0

The solution

My solution (or rather workaround) is to create a new file that will contain the same classes names as in the main files of the project. I created a special directory for that.

There I can easily declare all the hidden properties I access via __get() (__set(), __isset() etc.) like this:

class myClass {
 /**
  * Some accessible hidden property.
  * @var string $foo
  */
  public $foo;

 /**
  * Another accessible hidden property.
  * @var bool $bar
  */
  public $bar;
}

And that's all.

This makes Eclipse allow help me by hinting the class's properties (it combines all properties of two same-named classes) and also gives doxygen the proper info for documentation.

The problem is doxygen will generate this file in its "Files" section; if you don't care, it's ok.

Please note that in my "documentation file" I did not need to re-type the doNothing() method as it's visible (because it's public).

Hope this helps to anyone.

Voitcus
  • 4,463
  • 4
  • 24
  • 40
0

More elegant would be use phpdoc @property in the same class file:

/**
 * Test class
 * @property foo $foo
 * @property bar $bar
 */
class myClass {
  private $field_someValue1;
  private $field_someValue2;
Amorphous
  • 779
  • 7
  • 27
  • It would be, and I'd seen this solution somewhere in the net, but it didn't work... Thanks for the tip, anyway – Voitcus May 27 '13 at 23:18