访问子类中的私有变量

时间:2014-12-30 00:47:15

标签: php class oop subclass

我上课了。

<?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

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

所以我的noob问题是可以以某种方式从子类访问$attributes吗?我在父类中没有看到__get或__set方法,所以我猜测没有。

开发人员正在将private属性更改为protected。这样就可以解决我访问属性的问题。

2 个答案:

答案 0 :(得分: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 :(得分: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 );
    }

    // ...
}