PHP:如何在子构造中调用父构造的私有值?

发布于 2024-10-27 10:06:56 字数 1166 浏览 2 评论 0原文

我希望能够在父构造函数中设置私有属性的值,并在子构造函数或方法中调用该值。

例如:

<?php


abstract class MainClass
{
    private $prop_1;
    private $prop_2;


     function __construct()
     {
            $this->prop_2 = 'this is  the "prop_2" property';
     }
}

class SubClass extends MainClass
{
    function __construct()
    {
        parent::__construct();
        $this->prop_1 = 'this is the "prop_1" property';
    }

    public function GetBothProperties()
    {
        return array($this->prop_1, $this->prop_2);
    }

}

$subclass = new SubClass();
print_r($subclass->GetBothProperties());

?>

输出:

Array
(
    [0] => this is the "prop_1" property
    [1] => 
)

但是,如果我将 prop_2 更改为 protected,输出将是:

Array
(
    [0] => this is the "prop_1" property
    [1] => this is  the "prop_2" property
)

I have basic Knowledge of OO and php, but我不明白是什么阻止 prop_2private 时被调用(或显示?);它不可能是私有/公共/受保护的问题,因为“prop_1”是私有的并且能够被调用和显示......对吗?

这是子类与父类中赋值的问题吗?

我希望能帮助您理解原因。

谢谢。

I want to be able to set a private attribute's value in the parent constructor, and call the value in a child's constructor or method.

For example:

<?php


abstract class MainClass
{
    private $prop_1;
    private $prop_2;


     function __construct()
     {
            $this->prop_2 = 'this is  the "prop_2" property';
     }
}

class SubClass extends MainClass
{
    function __construct()
    {
        parent::__construct();
        $this->prop_1 = 'this is the "prop_1" property';
    }

    public function GetBothProperties()
    {
        return array($this->prop_1, $this->prop_2);
    }

}

$subclass = new SubClass();
print_r($subclass->GetBothProperties());

?>

Output:

Array
(
    [0] => this is the "prop_1" property
    [1] => 
)

However, if I change prop_2 to protected, the output will be:

Array
(
    [0] => this is the "prop_1" property
    [1] => this is  the "prop_2" property
)

I have basic knowledge of OO and php, but I can't figure out what is preventing prop_2 from being called (or shown?) when it's private; it can't be a private/public/protected issue, since 'prop_1' is private and able to be called and shown... right?

Is it an issue of assigning the values in the child class vs parent class?

I would appreciate help in understanding why.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我一直都在从未离去 2024-11-03 10:06:56

子类中不能访问父类的私有属性,反之亦然。

你可以这样做

abstract class MainClass
{
   private $prop_1;
   private $prop_2;


   function __construct()
   {
        $this->prop_2 = 'this is  the "prop_2" property';
   }

   protected function set($name, $value)
   {
        $this->$name = $value;
   }

   protected function get($name)
   {
      return $this->$name;
   }

 }


class SubClass extends MainClass
{
    function __construct()
    {
        parent::__construct();
        $this->set('prop_1','this is the "prop_1" property');
    }

    public function GetBothProperties()
    {
        return array($this->get('prop_1'), $this->get('prop_2'));
    }

}

Private properties of parent class can not be accessed in Child class and vice versa.

You can do like this

abstract class MainClass
{
   private $prop_1;
   private $prop_2;


   function __construct()
   {
        $this->prop_2 = 'this is  the "prop_2" property';
   }

   protected function set($name, $value)
   {
        $this->$name = $value;
   }

   protected function get($name)
   {
      return $this->$name;
   }

 }


class SubClass extends MainClass
{
    function __construct()
    {
        parent::__construct();
        $this->set('prop_1','this is the "prop_1" property');
    }

    public function GetBothProperties()
    {
        return array($this->get('prop_1'), $this->get('prop_2'));
    }

}
无尽的现实 2024-11-03 10:06:56

如果要从子类访问父类的属性,则必须将父类的属性设置为 protected NOT private。这样它们仍然无法从外部访问。
您无法按照您尝试的方式在子类中覆盖父级的私有属性可见性。

If you want to access the parent's properties from the child class, you must make the parent's properties protected NOT private. This way they are still inaccessible externally.
You can't override the parent's private properties visibility in the child class in the way you are trying to.

怪我闹别瞎闹 2024-11-03 10:06:56

正如其他人所指出的,您需要将父级的属性更改为“受保护”。但是,另一种方法是为您的父类实现一个 get 方法,该方法允许您访问该属性,或者如果您希望能够访问该属性,则实现一个 set 方法。 -骑它。

因此,在您的父类中,您需要声明:

protected function setProp1( $val ) {
  $this->prop_1 = $val;
}

protected function getProp1() {
  return $this->prop_1;
}

然后,在您的子类中,您可以访问 $this->setProp1("someval");$val = $this ->getProp1(),分别。

As others have noted, you'd need to change the parent's properties to protected. However, the other way is by implementing a get method for your parent class, which allows you access to the property, or implementing a set method if you want the ability to over-ride it.

So, in your parent class, you'd declare:

protected function setProp1( $val ) {
  $this->prop_1 = $val;
}

protected function getProp1() {
  return $this->prop_1;
}

Then, in your child class, you can access $this->setProp1("someval"); and $val = $this->getProp1(), respectively.

下雨或天晴 2024-11-03 10:06:56

我在这里找到了一个与 lambda 一起使用的简单技巧: https ://www.reddit.com/r/PHP/comments/32x01v/access_private_properties_and_methods_in_php_7/

基本上,您使用 lambda 并将其绑定到实例,然后您可以访问它的私有方法和属性

有关 lambda 调用的信息: https://www.php.net/manual/en/closure.call。 php

There is a simple trick you an use with lambdas, which I found here: https://www.reddit.com/r/PHP/comments/32x01v/access_private_properties_and_methods_in_php_7/

basically you use a lambda and bind it to the instance and then you can access it's private methods and properties

Info on the lambda call: https://www.php.net/manual/en/closure.call.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文