ActiveState Community

Autocomplete private members of a class

Posted by abelbp on 2009-04-29 20:59
OS: All / Any

Hi, i really want to know if there is a way that the autocomplete dropdown list can show public and private members (variables and functions) of classes.

(excuse my poor english)

toddw | Thu, 2009-04-30 10:03

I'm guessing that this is for PHP, yes?

Komodo should always be showing the public class members, but the protected and private members will only be shown if your inside the class scope.

There is no way to alter this behavior (without modifying code).

Could you explain why you need to see the protected/private members when outside of the class scope?

Thanks,
Todd

abelbp | Thu, 2009-04-30 17:16

That's correct Todd, is for PHP. I'm using de "magic" methods __get and __set for accessing the private variables when i need to do some validation before assign values to them.

I know the usual way for accessing private variables is like this:

<?php
class Person{
    private $name; //Need to be a String

    public function setName($value){
        if (!is_string($value))
            throw new Exception("I expect a String");
        else
            $this->name = $value;
    }
}

$newPerson = new Person;
$newPerson->setName("Fernando"); //<-- this way of assign values feels odd
?>

using a function to get or set the variable. The way i do is something like this:

<?php
class Person{
    private $name; //Need to be a String (this var it's with the porpuse of
                   //autocomplete show the name of the var in his dropdownlist,
                   //it would be erased in the final version of the class)

    private $all_vars = array('name' => null);
    
    private function __set($var_name, $value){
        if(!array_key_exists($var_name,$this->all_vars))
            throw new Exception("Unknow Property $var_name");
        elseif(method_exists($this,'set'.$var_name))
            return call_user_func(array($this, 'set'.$var_name), $value);
        else
            $this->all_vars[$var_name] = $value;
    }
    
    private function setName($value){
        if (!is_string($value))
            throw new Exception("I expect a String");
        else
            $this->all_vars['name'] = $value;
    }
}

$newPerson = new Person;
$newPerson->name = "Fernando"; //<-- this is a more natural way
                               //    to assign values....I think.
?>

Now, i think this is more natural way to do assings, but it has an inconvenience, i need (or some one else) to memorize and always type the entire names of the private variables.

This situation makes i can't take advantage of the autocomplete features of Komodo Edit and that's the reason i'm asking if there is a way i can make the autocomplete showme the private variables.

Thanks,

Abel

toddw | Thu, 2009-04-30 17:35

Hi Abel,

Komodo can support this through the use of PHPDoc "@property" tags at the class level. So, in your example something like this:

<?php
/**
 * @property string $name
 * @property string $surname
 */

class Person {}
$newPerson = new Person;
$newPerson->   // will show the name/surname attributes here
<?php
/**
 * @property string $name
 * @property string $surname
 */
class Person {}
$newPerson = new Person;
$newPerson->   // will show the name/surname attributes here

For more details see here:
http://bugs.activestate.com/show_bug.cgi?id=72960

Cheers,
Todd

abelbp | Thu, 2009-04-30 18:18

Thank you so much Todd, that's exactly what i'm looking for, now i think i owe you a beer :P

Abel

toddw | Fri, 2009-05-01 12:41

Great, glad to hear the PHPDoc workaround helps :)