简体   繁体   English

是否可以像在Java中那样在PHP中访问隐藏的实例字段?

[英]Is it possible to access hidden instance fields in PHP as in Java?

In Java as in PHP, instance fields declared in parent classes are inherited in child classes. 在Java和PHP中一样,在父类中声明的实例字段在子类中继承。 Therefore, it is possible to access them using the keyword this (resp. $this in PHP). 因此,可以使用关键字this (在PHP中为$this )访问它们。

In Java, if the child class happens to declare a field with the same name as the parent class, the parent class's field is hidden (see JLS 8.3.1.1-3: Hiding of Instance Variables ). 在Java中,如果子类碰巧声明了一个与父类同名的字段,则父类的字段将被隐藏 (请参见JLS 8.3.1.1-3:隐藏实例变量 )。 It is there, but if the child class wants to access it, it needs to use the keyword super , as in the following example: 它在那里,但是如果子类想要访问它,则需要使用关键字super ,如以下示例所示:

class Foo {
    protected int x = 1;
}

class Bar extends Foo {
    protected int x = 2;

    void somemethod() {
        System.out.println(this.x); // prints 2
        System.out.println(super.x); // prints 1
    }
}

Please note that I consider hiding fields as a bad practice, as it only makes for confusing code and easily leads to logic bugs. 请注意,我认为隐藏字段是一种不好的做法,因为这只会使代码混乱,并且容易导致逻辑错误。 It is best not to declare instance fields with the same name as an instance field of a parent class. 最好不要声明与父类的实例字段同名的实例字段。 Therefore, this question is of a purely academic nature. 因此,这个问题纯属学术性质。

Nevertheless I am curious: Is it in principle possible to access hidden instance fields in PHP, as is possible in Java? 不过,我很好奇:原则上是否可以像Java中那样访问PHP中的隐藏实例字段? Would it be possible to translate the above code snippet to PHP? 是否可以将上述代码片段转换为PHP?

<?php

class Foo {
    protected $x = 1;
}

class Bar extends Foo {
    protected $x = 2;

    function somemethod() {
        echo $this->x, PHP_EOL; // prints 2
        //echo $parent->x, PHP_EOL; // doesn't compile... is there a way to do it?
    }
}

Please note that I am talking about instance variables here. 请注意,我在这里谈论实例变量。 Class (ie, static) variables are a different story altogether and are not a subject of this question. 类(即静态)变量完全是另一回事,而不是这个问题的主题。

If it is not possible: Does this mean that the parent instance field effectively gets overwritten in PHP (instead of just hidden), and is thus inaccessible? 如果不可能:这是否意味着父实例字段实际上已在PHP中被覆盖 (而不仅仅是隐藏),因此无法访问?

The reason why this is not working for you, is because you are overriding the parent. 之所以对您不起作用,是因为您要覆盖父级。 Foo->$x is available in the child scope, therefor when you define X as 2 in Bar it overrides the previous set variable. Foo->$x在子作用域中可用,因此,当您在Bar中将X定义为2 ,它将覆盖先前的设置变量。

If you want something like this to work like in Java, then you would have to use constants. 如果您希望像这样的东西在Java中工作,则必须使用常量。

class Foo {
    const x = 1;
}

class Bar extends Foo {
    const x = 2;

    function somemethod() {
        echo parent::x; // prints 1
        echo self::x; // prints 2
    }
}

Or you could simply change the variable name in Bar and then you would have direct access to it through inheritance. 或者,您可以简单地在Bar更改变量名,然后可以通过继承直接访问它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM