PHP 为什么会在对象的注释里面声明属性和方法,方法没有内容,他的作用是什么

/**
 * Modules\Sword\Models\Group
 *
 * @property int $id
 * @property string $group_name 分组名称
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|\Modules\Sword\Models\Group groupName($groupName)
 * @method static \Illuminate\Database\Eloquent\Builder|\Modules\Sword\Models\Group newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\Modules\Sword\Models\Group newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\Modules\Sword\Models\Group query()
 * @method static \Illuminate\Database\Eloquent\Builder|\Modules\Sword\Models\Group whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\Modules\Sword\Models\Group whereGroupName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\Modules\Sword\Models\Group whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\Modules\Sword\Models\Group whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class Group extends Model
{
    .
    .
    .
}

今天看公司里面的项目,在一个模型文件里,调用这注释里面的方法有啥用, 没有方法体,里面的逻辑是啥返回啥,我懵了,这又是啥技术。。。。

《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
最佳答案

这个就是注解,举个例子,这是一个支付包

Pay::alipay($config)

类中的注释,但里面是没有这两个方法的

/**
 * @method static Alipay alipay(array $config) 支付宝
 * @method static Wechat wechat(array $config) 微信
 */

里面有个魔术方法,不存在alipay这个方法会自动调用,这个魔术方法里实例化了一个对象,路径是Gateways\Alipay.php

    /**
     * Magic static call.
     *
     * @author yansongda <me@yansongda.cn>
     *
     * @param string $method
     * @param array  $params
     *
     * @throws InvalidGatewayException
     * @throws Exception
     */
    public static function __callStatic($method, $params): GatewayApplicationInterface
    {
        $app = new self(...$params);

        return $app->create($method);
    }

    /**
     * Create a instance.
     *
     * @author yansongda <me@yansongda.cn>
     *
     * @param string $method
     *
     * @throws InvalidGatewayException
     */
    protected function create($method): GatewayApplicationInterface
    {
        $gateway = __NAMESPACE__.'\\Gateways\\'.Str::studly($method);

        if (class_exists($gateway)) {
            return self::make($gateway);
        }

        throw new InvalidGatewayException("Gateway [{$method}] Not Exists");
    }
2年前 评论
讨论数量: 6

可能是 注解

2年前 评论
aab

可能是为了辅助 IDE 的工具,同时也告诉其他开发,这里边可能会有这么多的属性和方法!特别是像 Laravel 这样喜欢使用魔术方法的框架。

2年前 评论

这个就是注解,举个例子,这是一个支付包

Pay::alipay($config)

类中的注释,但里面是没有这两个方法的

/**
 * @method static Alipay alipay(array $config) 支付宝
 * @method static Wechat wechat(array $config) 微信
 */

里面有个魔术方法,不存在alipay这个方法会自动调用,这个魔术方法里实例化了一个对象,路径是Gateways\Alipay.php

    /**
     * Magic static call.
     *
     * @author yansongda <me@yansongda.cn>
     *
     * @param string $method
     * @param array  $params
     *
     * @throws InvalidGatewayException
     * @throws Exception
     */
    public static function __callStatic($method, $params): GatewayApplicationInterface
    {
        $app = new self(...$params);

        return $app->create($method);
    }

    /**
     * Create a instance.
     *
     * @author yansongda <me@yansongda.cn>
     *
     * @param string $method
     *
     * @throws InvalidGatewayException
     */
    protected function create($method): GatewayApplicationInterface
    {
        $gateway = __NAMESPACE__.'\\Gateways\\'.Str::studly($method);

        if (class_exists($gateway)) {
            return self::make($gateway);
        }

        throw new InvalidGatewayException("Gateway [{$method}] Not Exists");
    }
2年前 评论

方便 IDE 和阅读有哪些方法可以调用

2年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!