PHP构造函数和扩展父类

z3yyvxxp  于 2022-11-28  发布在  PHP
关注(0)|答案(1)|浏览(109)

我来自Java,希望像在Java中那样使用子类的构造函数,但在PHP中似乎不是这样。子对象的构造函数似乎不能覆盖父对象中构造的示例变量值。这里,父对象只被用于多个子类之间的公共方法,这些方法将扩展它。2父类永远不会被单独调用。
当我示例化一个新的TestPackage时,我只是从父类中得到初始化的错误值。

<?php
interface Package{
    public function getCode();
    public function decode($packageCode);
    public function getSetName();
    public function getSetNameLang();
    public function getSubsetType();
    public function getSubsetTypeLang();
    public function setSubsetType($typeCode);
    public function getPackageOptionsArray();
    public function getChoicesArray();
    public function getOptionStatus($index);
    public function setOptionStatus($index, $booleanStatus);
}
?>

ParentPackage.php

class ParentPackage implements Package{
    private int $packageCode;
    private int $subsetType;
    private String $setName;
    private String $setNameLang;
    private $packageOptionsArray;
    private $subsetTypeArray;

    public function __construct(){
        $this->packageCode = 0;
        $this->subsetType = 0;
        $this->setName = "Error: parentClass";
        $this->setNameLang = "Error: Parent Class";

        $this->packageOptionsArray = array("Error",   //too much stuff to pass
                                            "Error",  //to a constructor as a variable
                                            "Error",
                                            "Error",
                                            "Error",
                                            "Error",
                                            "Error",
                                            "Error",
                                            "Error",
                                            "Error",
                                            "Error",
                                            "Error",
                                            );
        
        $this->subsetTypeArray = array("Error: Initializer",   //16
                                        "Error: Initializer",  //32
                                        "Error: Initializer",  //48
                                        );

     //........A whole bunch more stuff that is too much to pass as a variable to a constructor
     //..... OR let's just say that I don't want to do it that way

    }

TestPackage.php

class TestPackage extends ParentPackage{

    public function __construct(){
        parent::__construct();
        $this->packageCode = 0;
        $this->subsetType = 0;
        $this->setName = "layeredarch";
        $this->setNameLang = "Layered Arch";

        $this->packageOptionsArray = array("Customized welcome sign (choice of trellis half arch or smooth half arch insert up to 25 words text)",
                                            "3 piece seating chart half arch set (print service for cards is available for a small additional fee)",
                                            "Table numbers 1-30",
                                            "Gold Card option04 with choice of Gifts & Cards sign",
                                            "5 Reserved signs",
                                            "Up to 2 Double Half Arch Small signs (Gifts & Cards, Take One, Dont Mind if I Do, In Loving Memory)",
                                            "Up to 2 Sunset Small signs (Please Sign Our Guestbook, Gifts & Cards, In Loving Memory)",
                                            "1 Double Half Arch Medium sign (Cheers, The Bar, Guestbook, or Custom Acrylic Text)",
                                            "1 Double Full Arch Medium sign (Signature Drinks, or Custom Acrylic Text)",
                                            "Unplugged Ceremony sign",
                                            "Hairpin Record Player Prop",
                                            "%22Mr & Mrs%22 Custom Head Table Keepsake is a free gift in addition to the items above"
                                            );
        
        $this->subsetTypeArray = array("Full Set",   //16
                                        "Pick Six",  //32
                                        "Pick Four" //48
                                        );

    }
hfyxw5xn

hfyxw5xn1#

private成员只能是本地成员,即不能通过子类对其进行读/写。parent 类中的任何setter/getter只能从 parent 范围内的成员进行读/写。
如果您将成员访问权限从private更改为protected,则此操作有效。
当子类设置它们时,PHP不会给予访问错误,因为它使用了PHP的“动态声明成员”特性。

class ParentPackage implements Package {
    protected int $packageCode;
    protected int $subsetType;
    protected string $setName;
    protected string $setNameLang;
    protected $packageOptionsArray;
    protected $subsetTypeArray;
...

次优解

另一个解决方案是将整个getter/setter方法从父类复制到子类中,有效地切换它们访问的成员范围:

class ParentPackage implements Package {
    private $packageOptionsArray;
    public function getPackageOptionsArray() { return $this->packageOptionsArray; }
}

class TestPackage extends ParentPackage {
    // Optional, but without this, this property is dynamically created and defaults to 'public'
    private $packageOptionsArray;
    /** Override */
    public function getPackageOptionsArray() { return $this->packageOptionsArray; }
}

相关问题