gpt4 book ai didi

PHP 继承 : child class overriding parent variable/property for use in constructor

转载 作者:可可西里 更新时间:2023-10-31 23:31:49 24 4
gpt4 key购买 nike

我有一个(抽象的)父类应该在构造期间提供功能。子类可以覆盖构造函数中使用的属性:

class Parent extends MiddlewareTest
{
// abstract channel properties
protected $title = NULL;
protected $type = NULL;
protected $resolution = NULL;

function __construct() {
parent::__construct();

$this->uuid = $this->createChannel($this->title, $this->type, $this->resolution);
}
}

class Child extends Parent
{
// channel properties
protected $title = 'Power';
protected $type = 'power';
protected $resolution = 1000;
}

问题是当未被覆盖的 Child::__construct() 运行时($this->createChannel 调用时,未使用被覆盖的属性NULL 参数)。

这在 PHP 中可行吗?还是我每次都必须求助于覆盖子构造函数来提供所需的功能?

注意:我看到了Properties shared between child and parents class in php但这是不同的,因为子属性不是在构造函数中分配的,而是根据定义分配的。

更新

事实证明我的测试用例有问题。由于 MiddlewareTest 基于 SimpleTest 单元测试用例,SimpleTest 实际上 - 我没有意识到 - 通过它的自动运行实例化了 Parent 类本身,这并不是故意的。通过使 Parent 类抽象化来修复。

经验教训:构建一个干净的测试用例,并在求助之前实际运行它。

最佳答案

我不确定您的服务器上发生了什么。我不得不对 MiddlewareTest 类做出假设,修改您的类名,并添加一些简单的调试行,但使用以下代码:

<?php
/**
* I'm not sure what you have in this class.
* Perhaps the problem lies here on your side.
* Is this constructor doing something to nullify those properties?
* Are those properties also defined in this class?
*/
abstract class MiddlewareTest {
// I assume this properties are also defined here
protected $title = NULL;
protected $type = NULL;
protected $resolution = NULL;
protected $uuid = NULL;

public function __construct()
{}

protected function createChannel($title, $type, $resolution)
{
echo "<pre>" . __LINE__ . ": "; var_export(array($this->title, $this->type, $this->resolution)); echo "</pre>";
echo "<pre>" . __LINE__ . ": "; var_export(array($title, $type, $resolution)); echo "</pre>";
return var_export(array($title, $type, $resolution), true);
}
}

// 'parent' is a keyword, so let's just use A and B
class A extends MiddlewareTest
{
// abstract channel properties
protected $title = NULL;
protected $type = NULL;
protected $resolution = NULL;

function __construct() {
parent::__construct();

echo "<pre>" . __LINE__ . ": "; var_export(array($this->title, $this->type, $this->resolution)); echo "</pre>";
$this->uuid = $this->createChannel($this->title, $this->type, $this->resolution);
echo "<pre>" . __LINE__ . ": "; var_export($this->uuid); echo "</pre>";
}
}

class B extends A
{
// channel properties
protected $title = "Power";
protected $type = "power";
protected $resolution = 1000;
}

$B = new B();
?>

我得到这些结果:

37: array (
0 => 'Power',
1 => 'power',
2 => 1000,
)

20: array (
0 => 'Power',
1 => 'power',
2 => 1000,
)

21: array (
0 => 'Power',
1 => 'power',
2 => 1000,
)

39: 'array (
0 => \'Power\',
1 => \'power\',
2 => 1000,
)'

正如您所看到的,结果是按照它们在实例化类中定义的方式传入的值,正如预期的那样。

您能否提供有关您的 MiddlewareTest 类的一些详细信息,这可能会阐明您可能遇到此行为的原因?

你运行的是什么版本的 php?

关于PHP 继承 : child class overriding parent variable/property for use in constructor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17998683/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com