简体   繁体   English

PHP OOP 在子类中重新声明私有方法/函数

[英]PHP OOP redeclare private method/function in child class

Inside php manual example http://php.net/manual/en/language.oop5.visibility.php#example-242 it is saying在 php 手册示例http://php.net/manual/en/language.oop5.visibility.php#example-242里面说

We can redeclare the public and protected method, but not private我们可以重新声明 public 和 protected 方法,但不能重新声明私有

what do they mean my that i might not be getting how to use inheritance properly but say we have this code.他们是什么意思我可能没有得到如何正确使用继承但说我们有这个代码。

class MyClass1 {
    public $public = 'Public 1';
    protected $protected = 'Protected 1';
    private $private = 'Private 1';

    function printHello1() {
        echo $this->public . " MyClass1 ". PHP_EOL;
        echo $this->protected . " MyClass1 " . PHP_EOL;
        echo $this->private . " MyClass1 " . PHP_EOL;
    }
}



class MyClass2 extends MyClass1 {
    public $public = 'Public 2';
    protected $protected = 'Protected 2';
    private $private = 'Private 2';

    function printHello2() {
        echo $this->public . " MyClass2 ". PHP_EOL;
        echo $this->protected . " MyClass2 " . PHP_EOL;
        echo $this->private . " MyClass2 " . PHP_EOL;
    }
}

$obj2 = new MyClass2();
$obj2->printHello1();
$obj2->printHello2();

will return会回来

Public 2 MyClass1
Protected 2 MyClass1
Private 1 MyClass1

Public 2 MyClass2 
Protected 2 MyClass2 
Private 2 MyClass2 

Seems like i am able to create another $private variable in MyClass2 without any problem, so why they say its we can't ?似乎我可以MyClass2MyClass2创建另一个$private变量,那么为什么他们说我们不能呢?

yes it does not change $private variable in MyClass1 when i use function printHello1() in parent class however when i run printHello2() in child class MyClass2 it does show new value for $private variable.是的,当我在父类中使用函数printHello1()时,它不会更改MyClass1 $private变量,但是当我在子类MyClass2运行printHello2() ,它确实显示了$private变量的新值。

Now question i have is this bad practice to:现在我的问题是这种不好的做法是:

  1. Overwrite/Redeclare private property function in child class?覆盖/重新声明子类中的私有属性函数?
  2. Create second function printHello2() in child class when there is one already in parent class, this makes code bit spaghettish isn't it?当父类中已经有一个函数时,在子类中创建第二个函数printHello2() ,这使代码有点意大利面,不是吗?
  3. Same exact logic apply s to private methods correct?将相同的确切逻辑应用于私有方法是否正确?

private is limited to THAT particular class and its code. private仅限于那个特定的类及其代码。 It is invisble to descendant classes.它对后代类是不可见的。 Eg if you eliminate the private $private in MyClass2:例如,如果您消除 MyClass2 中的private $private

class MyClass2 extends MyClass1 {
    // private $private = 'Private 2';
    ^^---literally the only change

and then run the code:然后运行代码:

Public 2 MyClass1
Protected 2 MyClass1
Private 1 MyClass1
Public 2 MyClass2
Protected 2 MyClass2
PHP Notice:  Undefined property: MyClass2::$private in /home/sites/ca.usask.vido-inout/html/z.php on line 25
 MyClass2

That's why you get Private 1 and Private 2 .这就是您获得Private 1Private 2 private variables aren't "shared" within the family.私有变量不在家族内“共享”。 You're not "overwriting" anything, because private 1 doesn't exist in the descendant classes.您不会“覆盖”任何内容,因为子类中不存在private 1

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

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