php / phpDoc - @return instance of $this class?

前端 未结 5 1415
温柔的废话
温柔的废话 2021-02-07 16:08

How do I mark a method as \"returns an instance of the current class\" in my phpDoc?

In the following example my IDE (Netbeans) will see that setSomething always returns

5条回答
  •  迷失自我
    2021-02-07 16:25

    Update:

    As of Netbeans 7.4, the IDE supports @return self, static, and this (http://wiki.netbeans.org/NewAndNoteworthyNB74#Editor_2).

    class foo {
        protected $_value = null;
    
        /**
         * Set something
         *
         * @param string $value the value
         * @return this
         */
        public function setSomething($value) {
            $this->_value = $value;
            return $this;
        }
    }
    
    class bar extends foo {
        public function someOtherMethod(){}
    }
    

    Previous Answer:

    We have a similar issue with a record iterator's current() method. Since the iterator is extended for many different classes, it doesn't make sense to have a @return $class associated with it. We've used @satrun77's Option 2 before, but I've used @method with some success in Netbeans.

    class foo {
        protected $_value = null;
    
        /**
         * Set something
         *
         * @param string $value the value
         * @return foo
         */
        public function setSomething($value) {
            $this->_value = $value;
            return $this;
        }
    }
    
    /**
     * @method bar setSomething($value)
     */
    class bar extends foo {
        public function someOtherMethod(){}
    }
    

提交回复
热议问题