首页 > 解决方案 > 有人可以在这里解释一下语法吗

问题描述

这是来自 WP 插件的一些代码。我对 OOP 相当陌生,而且我实际上并不熟悉函数参数中的 \ ,它看起来像是在实例化一个类。有人想澄清一下吗?

 public function customize_membership_plan_row_actions( $actions, \WP_Post $post ) {
        global $typenow;

这是完整的功能供参考。

    /**
     * Customizes membership plan row actions.
     *
     * @since 1.0.0
     *
     * @param array $actions array of post actions
     * @param \WP_Post $post post object
     * @return array
     */
    public function customize_membership_plan_row_actions( $actions, \WP_Post $post ) {
        global $typenow;

        if ( 'wc_membership_plan' !== $typenow ) {
            return $actions;
        }

        // add view as member action
        $actions['view_as_member'] = '<a href="' . wp_nonce_url( admin_url( 'edit.php?post_type=wc_membership_plan&action=view_as_member&amp;post=' . $post->ID ), 'wc-memberships-view-as-member-of_' . $post->ID ) . '" title="' . __( 'View site as a member of this plan', 'woocommerce-memberships' ) . '" rel="permalink">' .
                                     __( 'View site as member', 'woocommerce-memberships' ) .
                                     '</a>';

        return $actions;
    }

标签: phpwordpress

解决方案


开头的反斜杠字符 () 表示 WP_Post 类型在全局命名空间中。您可以在此处了解有关 PHP 命名空间的更多信息


推荐阅读