php - 访问子类中的私有(private)变量

标签 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()

我认为这是因为父类定义了 $attributesprivate .

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

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

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

最佳答案

您可以使用反射:

// 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 实例的副本,以便根据需要重用。

关于php - 访问子类中的私有(private)变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27698142/

相关文章:

php - 我如何在 ubuntu 上使守护进程崩溃

Javascript访问父公共(public)变量

java - 静态内部类与 Companion 的内部类

oop - 在方法名称中写入 "Not"是一个不好的做法吗?

Javascript 正则表达式相当于 PHP 正则表达式

php - 如何将php中elseif的结果获取到jquery

c++ - 在类中创建对象的最佳实践

java - 调用 Intent 时应用程序关闭

C# 检查一个类是否是 X,或者是从 X 继承的

php - 如何配置 OneUpUploaderBundle 和 OneUpFlysystemBundle 在 Symfony 4.1 中工作