在子类访问私有变量

问题描述 投票:1回答:2

我有一个类。

<?php

class WC_Swatch_Picker {

private $size;
private $attributes;
private $selected_attributes;
private $swatch_type_options;

public function __construct( $product_id, $attributes, $selected_attributes ) {
    $this->swatch_type_options = maybe_unserialize( get_post_meta( $product_id, '_swatch_type_options', true ) );

    if ( !$this->swatch_type_options ) {
        $this->swatch_type_options = array();
    }

    $product_configured_size = get_post_meta( $product_id, '_swatch_size', true );
    if ( !$product_configured_size ) {
        $this->size = 'swatches_image_size';
    } else {
        $this->size = $product_configured_size;
    }

    $this->attributes = $attributes;
    $this->selected_attributes = $selected_attributes;
}

public function picker() {
    ?>

    <table class="variations-table" cellspacing="0">
        <tbody>
            <?php
            $loop = 0;
            foreach ( $this->attributes as $name => $options ) : $loop++;
                $st_name = sanitize_title( $name );
                $hashed_name = md5( $st_name );
                $lookup_name = '';
                if ( isset( $this->swatch_type_options[$hashed_name] ) ) {
                    $lookup_name = $hashed_name;
                } elseif ( isset( $this->swatch_type_options[$st_name] ) ) {
                    $lookup_name = $st_name;
                }
                ?>
                <tr>
                    <td class="label"><label for="<?php echo $st_name; ?>"><?php echo WC_Swatches_Compatibility::wc_attribute_label( $name ); ?></label></td>
                    <td>
                        <?php
                        if ( isset( $this->swatch_type_options[$lookup_name] ) ) {
                            $picker_type = $this->swatch_type_options[$lookup_name]['type'];
                            if ( $picker_type == 'default' ) {
                                $this->render_default( $st_name, $options );
                            } else {
                                $this->render_picker( $st_name, $options, $name );
                            }
                        } else {
                            $this->render_default( $st_name, $options );
                        }
                        ?>
                    </td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
    <?php
}

我想以扩展类,这样我可以输出作为picker()显示<table>代替<div>方法。

这里是我的尝试,以便扩展类。

class SSi_WC_Swatch_Picker extends WC_Swatch_Picker {

public function picker() {
    ?>

    <div class="variations-table">

            <?php

            $loop = 0;
            foreach ( $this->attributes as $name => $options ) : $loop++;
                $st_name = sanitize_title( $name );
                $hashed_name = md5( $st_name );
                $lookup_name = '';
                if ( isset( $this->swatch_type_options[$hashed_name] ) ) {
                    $lookup_name = $hashed_name;
                } elseif ( isset( $this->swatch_type_options[$st_name] ) ) {
                    $lookup_name = $st_name;
                }
                ?>
                <div>
                    <div class="label"><label for="<?php echo $st_name; ?>"><?php echo WC_Swatches_Compatibility::wc_attribute_label( $name ); ?></label></div>
                    <div>
                        <?php
                        if ( isset( $this->swatch_type_options[$lookup_name] ) ) {
                            $picker_type = $this->swatch_type_options[$lookup_name]['type'];
                            if ( $picker_type == 'default' ) {
                                $this->render_default( $st_name, $options );
                            } else {
                                $this->render_picker( $st_name, $options, $name );
                            }
                        } else {
                            $this->render_default( $st_name, $options );
                        }
                        ?>
                    </div>
                </div>
            <?php endforeach; 

            ?>

    </div>

    <?php
}

}

我在屏幕上显示出一个<div>像我想,但我得到:Notice: Undefined property: SSi_WC_Swatch_Picker::$attributesWarning: Invalid argument supplied for foreach()

我认为,这是因为父类$attributes定义为private

不幸的是我无法改变的父类。

所以,我的小白问题是可以在$attributes从子类莫名其妙访问?我看不出在父类,所以我猜没有一个__get或__set方法。

开发商正在改变private属性protected。这样就解决了我的访问性能的问题。

php class oop subclass
2个回答
1
投票

您可以使用反射:

// setup a reflector for WC_Swatch_Picker::size property
$ref = new ReflectionProperty("WC_Swatch_Picker", "size");
$ref->setAccessible(true);

// read the private "size" property
$size = $ref->getValue($this);

// update the private "size" property
$ref->setValue($this, $size);

注:这是有点低效率的,所以如果你打算这样做了很多,你应该保持ReflectionProperty实例的副本根据需要进行再利用。


1
投票

另一种可能性是重写构造你的子类,并设置自己的属性$attributes

class SSi_WC_Swatch_Picker extends WC_Swatch_Picker {

    private $attributes;

    public function __construct( $product_id, $attributes, $selected_attributes ) {
        $this->attributes = $attributes;

        // Call the parent constructor.
        parent::__construct( $product_id, $attributes, $selected_attributes );
    }

    // ...
}
© www.soinside.com 2019 - 2024. All rights reserved.