
在php中,当一个类定义了private(私有)属性时,这些属性只能在该类的内部被访问。子类虽然继承了父类的私有属性,但不能直接访问它们。通常,我们会通过在父类中定义public(公共)的构造函数__construct()来在对象实例化时初始化这些私有属性。然而,有时我们可能希望在不使用构造函数的情况下,或在对象实例化之后再进行属性的设置。
考虑以下示例代码,它尝试在没有定义构造函数的情况下,通过实例化子类时传递参数来初始化父类的私有属性:
<?php
class Fruit {
private $name;
private $color;
// 原始代码中尝试用此方法设置属性,但不是构造函数
public function patients($name, $color) {
$this->name = $name;
$this->color = $color;
}
public function intro() {
echo "The fruit is {$this->name} and the color is {$this->color}.";
}
}
class Strawberry extends Fruit {
public function message() {
echo $this->intro();
}
}
// 错误示范:试图在没有构造函数的情况下传递参数
$strawberry = new Strawberry("Strawberry", "red"); // 这会导致PHP错误
$strawberry->message();
?>上述代码中,new Strawberry("Strawberry", "red") 会导致一个致命错误,因为Strawberry类(及其父类Fruit)都没有定义一个接受两个参数的__construct方法。在没有显式构造函数时,PHP会提供一个默认的无参数构造函数。因此,尝试在实例化时传递参数是不允许的。
解决上述问题的核心在于,通过父类中定义的公共方法来间接设置私有属性。这些公共方法充当了私有属性的“守门人”,允许在对象实例化后,以受控的方式修改属性值。
为了提高代码的可读性和语义性,我们将原有的patients()方法重命名为更具描述性的describe()。
立即学习“PHP免费学习笔记(深入)”;
<?php
class Fruit {
private $name;
private $color;
// 定义一个公共方法来设置私有属性
public function describe($name, $color) {
$this->name = $name;
$this->color = $color;
}
public function intro() {
echo "The fruit is {$this->name} and the color is {$this->color}.";
}
}
class Strawberry extends Fruit {
public function message() {
echo $this->intro();
}
}
// 正确的初始化和使用方式
$strawberry = new Strawberry(); // 实例化时不再传递参数
$strawberry->describe("Strawberry", "red"); // 通过公共方法设置属性
$strawberry->message(); // 调用子类方法,该方法内部调用父类方法
?>运行上述代码,将输出 The fruit is Strawberry and the color is red.。
工作原理:
子类Strawberry继承了父类Fruit的所有公共(public)和受保护(protected)方法。这意味着,Strawberry的实例可以直接调用这些方法。
在上述例子中,Strawberry类中定义了message()方法,其唯一作用是调用父类的intro()方法。实际上,如果intro()方法的功能已经足够,message()方法并非必须。我们可以直接通过子类实例调用父类的公共方法:
<?php
class Fruit {
private $name;
private $color;
public function describe($name, $color) {
$this->name = $name;
$this->color = $color;
}
public function intro() {
echo "The fruit is {$this->name} and the color is {$this->color}.";
}
}
class Strawberry extends Fruit {
// message() 方法可以被移除,如果它的功能只是简单地调用父类方法
// public function message() {
// echo $this->intro();
// }
}
$strawberry = new Strawberry();
$strawberry->describe("Strawberry", "red");
$strawberry->intro(); // 直接调用父类的公共方法
?>这段代码同样会输出 The fruit is Strawberry and the color is red.。这展示了子类实例直接调用父类公共方法的灵活性。是否在子类中封装一层方法(如message())取决于具体的设计需求,例如是否需要为子类添加额外的逻辑或改变父类方法的行为。
在PHP中,即使没有显式定义构造函数,我们仍然可以通过在父类中提供公共的设置方法来初始化其私有属性。这种方法允许在对象实例化后灵活地设置属性,并通过继承机制确保子类实例能够访问和利用这些已设置的属性。理解private访问修饰符的特性以及如何通过公共接口与其交互,是编写健壮和可维护PHP面向对象代码的关键。
以上就是PHP面向对象:不使用构造函数初始化父类私有属性的技巧的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号