doulin6088 2014-09-18 15:59
浏览 33
已采纳

如何使用覆盖方法在PHP中显示setter内容?

this is my code:

<?php
    class person {
                var $name;      
                function __construct($persons_name) {
                        $this->name = $persons_name;
                }

                public function get_name() {
                        return $this->name;
                }
                protected function set_name($new_name) {
                    if ($this->name !=  "Jimmy Two Guns") {
                        $this->name = strtoupper($new_name);
                    }
                }
    }
    class employee extends person {
        protected function set_name($new_name) {
                if ($new_name ==  "Stefan TRALA") {
                    $this->name = $new_name;
                }

                else if ($new_name ==  "Johnny Fingers") {
                    person::set_name($new_name);
                }        
        }

        function __construct($employee_name) {
                $this->set_name($employee_name);
        }
    }

    $empl = new employee;
    echo $empl->set_name("Stefan TRALA");
?>

Im getting this warning: Warning: Missing argument 1 for employee::__construct() WHY ? why my echo does not work ? What did I write wrong ? Thank you!

  • 写回答

5条回答 默认 最新

  • dtn36013 2014-09-18 16:01
    关注

    Your constructor takes one argument, so it should be:

    $empl = new employee("Stefan TRALA");
    

    Then you can remove echo $empl->set_name() since it is already called in the constructor.


    how to get the name?

    add this function:

    function get_name(){
      return $this->name;
    }
    

    then use it:

    echo $empl->get_name();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 hexo+github部署博客
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?