NuSphere Forums Forum Index
NuSphere Forums
Reply to topic
Static Method Return Values - Auto Completion


Joined: 16 Jun 2011
Posts: 31
Reply with quote
Hi,

another case for that i don't find a working solution

Code:
class Test{

    /**
    * Return a instance of the called class
    *
    * @return self
    */
    static function getSmth(){
        $class = get_called_class();
        return new $class;
    }   
}

class Ext extends Test{
    public function someFunction(){}
}

Ext::getSmth()->...


As you can see, NuSphere detect the class "Test" for "Ext::getSmth() " for the auto completion, but i expect the "Ext" which i tried to emulate with "self"
I tried it with "@return $this", "@return static", "@return self". But nothing of that works for me.

Is there any hint for this case in NuSphere? I searched a lot in PHPDoc forums and other editor forums but i don't find a standard for that or any working solution.
View user's profileFind all posts by brainSend private message
Guru master

Joined: 24 Jul 2009
Posts: 737
Reply with quote
I'm unsure as to exactly what you want but ...

get_called_class() from within a static function would return Test so @return self looks correct and code completion appears to be working correctly; it is only showing getSmth() as an available method on getSmth() and should not be showing any methods for Ext.

If you want getSmth() to return a new instance of Ext then getSmth() should not be static.

Code:
class Test{

    /**
    * @return self
    *
    */
    function getSmth(){
        $class = get_called_class();
        return new $class;
    }   
}

/** @method Ext getSmth */
class Ext extends Test{
    public function someFunction(){}
}

Ext::getSmth()->


I know I might have mistunderdood what you need; I'm just trying to help Smile
View user's profileFind all posts by plugnplaySend private message


Joined: 16 Jun 2011
Posts: 31
Reply with quote
Hi

No, get_called_class always return the class from that you do the method call.
__CLASS__ always return the class your method is actually in.

Code:
class Test{

    /**
    * @return self
    *
    */
    static function getSmth(){
        var_dump(get_called_class(), __CLASS__);
    }
}

class Ext extends Test{
    public function someFunction(){}
}
Ext::getSmth();


Generate
Code:
string(3) "Ext" string(4) "Test"


Ant with "self" i tried to emulate that it always take the class from which you call instead of the class from which the method is in.
Ohterwise i would write the fixed "@return Test" if i always expect the "Test" instance as a result.

In other Editors they replace "@return CLASS_NAME" (very bad handling i guess) with the get_called_class() but nusphere also don't understand this way.

And thanks, i alreay know the "trick" with the @method but it's not nice coding when you need to add this documentation to every new subclass.
View user's profileFind all posts by brainSend private message


Joined: 06 Aug 2007
Posts: 13
Reply with quote
phpDoc already differentiates "@return $this" and "@return self", as well as some other IDEs.
phpDoc manual doesn't reflect it yet, but will be updated soon (issue #800 in phpDocumentor repository).

So could you please make sure PhpEd understands the following phpDoc correctly? It's really very clear and logical feature for classes definition.

class ParentClass {
/** @return self */
public static function getSelf() {}
/** @return static */
public static function getStatic() {}
/** @return $this */
public static function getThis() {}
}
class ChildClass extends ParentClass {}

ChildClass::getSelf(); //ParentClass
ChildClass::getStatic(); //ChildClass
ChildClass::getThis(); //ChildClass

Thank you in advance!
View user's profileFind all posts by shimasoftSend private message
Static Method Return Values - Auto Completion
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT - 5 Hours  
Page 1 of 1  

  
  
 Reply to topic