简体   繁体   English

如何将公共值从父 class 传递给 PHP 中的子 class?

[英]How to pass public value from parent class to child class in PHP?

I just learn deeper about OOP in PHP, but not an expert yet.我只是在 PHP 中更深入地了解 OOP,但还不是专家。 I'm still a beginner.我还是个初学者。 I have a problem.我有个问题。 I want to pass the public value from parent class to child class.我想将公共值从父 class 传递给子 class。 But I cant get it.但我无法得到它。 I tried to find the answer in google and stackoverflow, and I have tried to follow the instruction, but it didnt work.我试图在 google 和 stackoverflow 中找到答案,并尝试按照说明进行操作,但没有奏效。

My code is like below:我的代码如下:

class A
{
    public $arrayperk;
    public $nilai;
    
    function __construct()
        {
            $this->nilai='This is bad';
        }
    function testParent()
        {
            return $this->arrayperk;
        }
    function testA()
        {
            return number_format(1000); 
        }
}

class B extends A
{
    function  coba()
        {
            return $this->testParent();
        }
    function  coba2()
        {
            return $this->testA();
        }
    function coba3()
        {
            return $this->nilai;
        }
}

The problem is that I want to pass the value of $arrayperk to the function inside class B .问题是我想将$arrayperk 的值传递给class B内的 function 。 But it did not work at all.但它根本没有用。

My code is like below:我的代码如下:

$arrayperk=Array( 1 => 'array1', 2 => 'array2');
$class_a=new A();
$class_a->arrayperk=$arrayperk;
$class_b=new B();

print_r($class_a->testParent()); //return array as needed.
print_r($class_b->coba()); //return nothing although it calls the parent class, and the parent work correctly if it is called like above. This is what I want to get and the problem I face.
echo $class_b->coba2(); //return 1,000 and it access the parent function
echo $class_b->coba3(); //return This is bad as written at parent class.

As you can see, the problem is at如您所见,问题出在

$class_b->coba()

It gives nothing even I have call it correctly to the parent class, but the other test works fine when accessing the parent class function.即使我正确地将其调用给父 class,它也没有给出任何结果,但是在访问父 class function 时,其他测试工作正常。

I am even unable to pass $this->arrayperk inside class B. It returns nothing.我什至无法在 class B 中传递$this->arrayperk 。它什么也不返回。 What am I missing?我错过了什么? I cant get both $this->arrayperk and a function inside parent class.我无法在父 class 中同时获得$this->arrayperk和 function。

Please help.请帮忙。 Thanks.谢谢。

You're instantiating 2 different classes, $class_a and $class_b .您正在实例化 2 个不同的类, $class_a$class_b Your're only setting the $arrayperk for $clas_a您只是为 $ $arrayperk设置$clas_a

$class_a=new A();
$class_a->arrayperk=$arrayperk;
$class_b=new B();

here when $class_b is instantiated, you don't give it any properties.在这里,当 $class_b 被实例化时,你没有给它任何属性。 A simple fix would be to just assign the property:一个简单的解决方法是只分配属性:

$class_b->arrayperk = $arrayperk;

Even though you're inheriting the parent method, the actual property $arrayperk , in your case, is empty so you need to set it for each instance individually.即使您继承了父方法,实际属性$arrayperk在您的情况下是空的,因此您需要为每个实例单独设置它。

Inheritance means you inherit the methods of the parent class, but each instance behaves as a separate unit. Inheritance 意味着您继承父 class 的方法,但每个实例都作为一个单独的单元运行。

If you do如果你这样做

$class_c = new B();
var_dump($class_c->arrayperk);

This value would also end up being NULL该值最终也将是 NULL

Also helps if you var_dump your values, instead of printing, since in this case you would see the value of arrayperk is NULL.如果你 var_dump 你的值而不是打印也有帮助,因为在这种情况下你会看到 arrayperk 的值是 NULL。

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

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