重写类';s方法在PHP中不起作用


Overriding a class's method not working in PHP

我已经和这个问题斗争了一个多星期了,但还没有解决它。我正在尝试扩展一个类并覆盖它的一些方法,这样我就可以在那里显示我自己的消息:

父类:

class Order {
 public function details( $echo = true ) {
    $cart = $this->get_cart();
    $currency = $this->get_meta( 'mp_payment_info->currency', '' );
    $cart     = $this->get_meta( 'mp_cart_items' );
    if ( ! $cart ) {
        $cart = $this->get_meta( 'mp_cart_info' );
    }
    /**
     * Filter the confirmation text
     *
     * @since 3.0
     *
     * @param string The current confirmation text.
     * @param MP_Order The order object.
     */
    $confirmation_text = apply_filters( 'mp_order/confirmation_text', '', $this );
    $confirmation_text = apply_filters( 'mp_order/confirmation_text/' . $this->get_meta( 'mp_payment_info->gateway_plugin_name' ), $confirmation_text, $this );
    $cart_contents = '';
    ob_start();
    ?>
    <?php if ( is_array( $cart ) ): ?>
        <?php foreach ( $cart as $product_id => $items ): ?>
            <?php foreach ( $items as $item ): ?>
                <?php $product = new MP_Product( $product_id ); ?>
                <div class="mp_cart_item" id="mp-cart-item-104">
                    <div class="mp_cart_item_content mp_cart_item_content-thumb"><img
                            src="<?php echo $product->image_url( false ) ?>"
                            width="75" height="75" style="max-height: 75px;">
                    </div>
                    <!-- end mp_cart_item_content -->
                    <div class="mp_cart_item_content mp_cart_item_content-title">
                        <h2 class="mp_cart_item_title">
                            <a href="<?php echo $item['url'] ?>"><?php echo $item['name'] ?></a>
                        </h2>
                        <?php
                        if ( $product->is_download() && mp_is_shop_page( 'order_status' ) ) {
                            echo '<a target="_blank" href="' . $product->download_url( get_query_var( 'mp_order_id' ), false ) . '">' . __( 'Download', 'mp' ) . '</a>';
                        }
                        ?>
                    </div>
                    <!-- end mp_cart_item_content -->
                    <div class="mp_cart_item_content mp_cart_item_content-price"><!-- MP Product Price -->
                        <div class="mp_product_price" itemtype="http://schema.org/Offer" itemscope=""
                             itemprop="offers">
                                <span class="mp_product_price-normal"
                                      itemprop="price"><?php echo mp_format_currency( '', $item['price'] ) ?></span>
                        </div>
                        <!-- end mp_product_price -->
                    </div>
                    <!-- end mp_cart_item_content -->
                    <div
                        class="mp_cart_item_content mp_cart_item_content-qty"><?php echo $item['quantity'] ?>
                    </div>
                    <!-- end mp_cart_item_content --></div><!-- end mp_cart_item -->
            <?php endforeach; ?>
        <?php endforeach; ?>
    <?php else: ?>
        <?php
        $cart->display( array(
            'echo'     => true,
            'view'     => 'order-status',
            'editable' => false,
        ) );
        ?>
    <?php endif; ?>
    <?php
    $cart_contents = ob_get_clean();
    $html = '
        <!-- MP Single Order Details -->
        <section id="mp-single-order-details" class="mp_orders">
            <div class="mp_order_details">
                <div class="mp_order">' .
            $this->header( false ) .
            '</div><!-- end mp_order -->' .
            $confirmation_text . '
                <div class="mp_order_cart">' .
            $cart_contents . '
                </div><!-- end mp_order_cart -->
                <div class="mp_order_address">' .
            $this->get_addresses() . '
                </div><!-- end mp_order_address -->
            </div><!-- end mp_order_details -->
        </section><!-- end mp-single-order-details -->';
    /**
     * Filter the order details
     *
     * @since 3.0
     *
     * @param string $html The current details.
     * @param MP_Order $this The current order object.
     */
    $html = apply_filters( 'mp_order/details', $html, $this );
    if ( $echo ) {
        echo $html;
    } else {
        return $html;
    }
    }
}

我的类扩展了这个:

 class My_Order extends Order {
   public function details( $echo = true ) {
    $cart = $this->get_cart();
    $currency = $this->get_meta( 'mp_payment_info->currency', '' );
    $cart     = $this->get_meta( 'mp_cart_items' );
    if ( ! $cart ) {
        $cart = $this->get_meta( 'mp_cart_info' );
    }
    /**
     * Filter the confirmation text
     *
     * @since 3.0
     *
     * @param string The current confirmation text.
     * @param MP_Order The order object.
     */
    $confirmation_text = apply_filters( 'mp_order/confirmation_text', '', $this );
    $confirmation_text = apply_filters( 'mp_order/confirmation_text/' . $this->get_meta( 'mp_payment_info->gateway_plugin_name' ), $confirmation_text, $this );
    $cart_contents = '';
    ob_start();
    ?>
    <?php if ( is_array( $cart ) ): ?>
        <?php foreach ( $cart as $product_id => $items ): ?>
            <?php foreach ( $items as $item ): ?>
                <?php $product = new MP_Product( $product_id ); ?>
                <div class="mp_cart_item" id="mp-cart-item-104">
                    <div class="mp_cart_item_content mp_cart_item_content-thumb"><img
                            src="<?php echo $product->image_url( false ) ?>"
                            width="75" height="75" style="max-height: 75px;">
                    </div>
                    <!-- end mp_cart_item_content -->
                    <div class="mp_cart_item_content mp_cart_item_content-title">
                        <h2 class="mp_cart_item_title">
                            <a href="<?php echo $item['url'] ?>"><?php echo $item['name'] ?></a>
                        </h2>
                        <?php
                        if ( $product->is_download() && mp_is_shop_page( 'order_status' ) ) {
                            echo '<a target="_blank" href="' . $product->download_url( get_query_var( 'mp_order_id' ), false ) . '">' . __( 'Download', 'mp' ) . '</a>';
                        }
                        ?>
                    </div>
                    <!-- end mp_cart_item_content -->
                    <div class="mp_cart_item_content mp_cart_item_content-price"><!-- MP Product Price -->
                        <div class="mp_product_price" itemtype="http://schema.org/Offer" itemscope=""
                             itemprop="offers">
                                <span class="mp_product_price-normal"
                                      itemprop="price"><?php echo mp_format_currency( '', $item['price'] ) ?></span>
                        </div>
                        <!-- end mp_product_price -->
                    </div>
                    <!-- end mp_cart_item_content -->
                    <div
                        class="mp_cart_item_content mp_cart_item_content-qty"><?php echo $item['quantity'] ?>
                    </div>
                    <!-- end mp_cart_item_content --></div><!-- end mp_cart_item -->
            <?php endforeach; ?>
        <?php endforeach; ?>
    <?php else: ?>
        <?php
        $cart->display( array(
            'echo'     => true,
            'view'     => 'order-status',
            'editable' => false,
        ) );
        ?>
    <?php endif; ?>
    <?php
    $cart_contents = ob_get_clean();
    $html = 'I JUST WANT TO REPLACE THIS';
    /**
     * Filter the order details
     *
     * @since 3.0
     *
     * @param string $html The current details.
     * @param MP_Order $this The current order object.
     */
    $html = apply_filters( 'mp_order/details', $html, $this );
    if ( $echo ) {
        echo $html;
    } else {
        return $html;
    }
}
}

我唯一想覆盖的是:$html = 'I JUST WANT TO REPLACE THIS';

我也尝试过覆盖这个类的其他方法,但都不起作用。

感谢

如果你能够操作基类,那么你可以创建一个受保护的或公共的方法,然后覆盖这个方法:

基本类别:

class Order {
    public function details( $echo = true ) {
    //...
    $html = applyMyAwesomeFilters('mp_order/details', $html, $this);
   //...
    }
    protected function applyMyAwesomeFilters($path, $html, $this) 
    {
         return apply_filters($path, $html, $this);
    }
}

然后重写继承类中的函数:

    class My_Order extends Order {
    protected function applyMyAwesomeFilters($path, $html, $this) 
    {
         //Do something totally different here...
         return $somethingAwesome;
    }
}