dongruoqiong9017 2014-01-27 04:47
浏览 33

如何在OOP PHP中使用数组参数覆盖超类构造函数

Hello i have a problem in OOP PHP. I try override constructor from superclass to subclass with array parameter in it. This is my superclass code :

    <?php
        class Person 
        {

            private $firstName;
            private $lastName;
            private $gender;
            private $dob;
            private $address;
            private $country;
            private $phone;
            private $occupation;

            public function __construct($array) {
                foreach($array as $key => $value) {
                    $this->$key = $value;
                }
            }


                public function setFirstName($firstName) {
                    $this->firstName = $firstName;
                }

                public function getFirstName() {
                    return $this->firstName;
                }
            // Another Getter and Setter
            // ...

And this is my subclass code :

<?php

    class Register extends Person
    {

        private $nickName;
        private $email;

        public function __construct($array) {
            parent::__construct($array);
        }

        // Getter and Setter
        // ...

But when i try to access nickName and email, i get an error like this :

Fatal error: Cannot access private property Register::$email in C:\xampp\htdocs\php\oop\class\person.class.php on line 16

And this is my test code :

<?php
    function __autoload($myClass) {
        require_once 'class/' . $myClass . '.class.php';
    }

    $data = array(
        'firstname'     => $_POST['first_name'],
        'lastname'      => $_POST['last_name'],
        'nickname'      => $_POST['nick_name'],
        'gender'        => $_POST['gender'],
        'dob'           => $_POST['dob'],
        'address'       => $_POST['address'],
        'zipcode'       => $_POST['zipcode'],
        'country'       => $_POST['country'],
        'phone_number'  => $_POST['phone_number'],
        'email'         => $_POST['email'],
        'occupation'    => $_POST['occupation']
    );


    $account = new Register($data);
    echo $account->getFirstName();

    // Proses DB
    // $db->insert($account);

?>

Any Sugesstion? Thank you.

Answer :

I already find solutions. Key name in array must same with class property. In array i write firstname and property firstName, the correct way is firstName in both, array and property.

  • 写回答

2条回答 默认 最新

  • dougong5817 2014-01-27 04:55
    关注

    One option is to make your private properties protected, so they can be accessed from a parent method.

    Another option is to initialize the subclass's properties in the subclass constructor, and remove those entries from the array:

    class Register extends Person
    {
    
        private $nickName;
        private $email;
    
        public function __construct($array) {
            if (isset($array(['nickName'])) {
                $this->nickName = $array['nickName'];
                unset $array(['nickName'];
            }
            if (isset($array(['email'])) {
                $this->email = $array['email'];
                unset $array(['email'];
            }
            parent::__construct($array);
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)