dtkp51520 2017-07-27 00:50
浏览 40

许多调用方法 - 注意[重复]

i try set many the same methods. The parameter is dependent of array element. If case: 1 - show '1' number, if case: 2 - show '2' number. I presents code:

<?php
    error_reporting(E_ALL); 
    ini_set('display_errors', 1);

    class Test {
        protected $target = array('1' ,'2');
        private $message;

        public function setTarget($target) {
            return $this->target = $target;
        }

        public function getTarget() {
            return $this;
        }   
        public function getMessage() {
            return $this->message;
        }
        public function set($target) {
            switch($target) {
                case '1':
                    $this->message = $this->setTarget($this->target[0]);
                break;
                case '2':
                    $this->message = $this->setTarget($this->target[1]);
                break;
            } 
            return 
                $this->message; $this->target;
        }

    }
    $foo = new Test;

    echo $foo->set(1);
    echo $foo->set(2);
    echo $foo->set(1);
    echo $foo->set(1);
    echo $foo->set(2);
    echo $foo->set(1);

The notice:

1 Notice: Uninitialized string offset: 1 in /opt/lampp/htdocs/oop/test/test.php on line 25

Notice: Uninitialized string offset: 0 in /opt/lampp/htdocs/oop/test/test.php on line 22

Notice: Uninitialized string offset: 0 in /opt/lampp/htdocs/oop/test/test.php on line 22

Notice: Uninitialized string offset: 1 in /opt/lampp/htdocs/oop/test/test.php on line 25

Notice: Uninitialized string offset: 0 in /opt/lampp/htdocs/oop/test/test.php on line 22

How fix this problem?

</div>
  • 写回答

1条回答 默认 最新

  • dongzhiyi2006 2017-07-27 04:19
    关注

    On the first set method call, setTarget changes the value of your target list to $this->target[0].

    Separate the list of targets from the current target. In the example below, I transformed the $message as the current target and the list of available targets is in $targets

    <?php
    error_reporting(E_ALL); 
    ini_set('display_errors', 1);
    
    class Test {
        protected $targets = array('1' ,'2');
    
        private $currentTarget;
    
        public function setTarget($target) {
            // set the current target, do not override $this->targets
            return $this->currentTarget = $target;
        }
    
        public function getTarget() {
            // why are you returning $this here
            // imo, should be $this->currentTarget;
            return $this;
        }   
        public function getMessage() {
            return $this->message;
        }
        public function set($target) {
            switch($target) {
                case '1':
                    $this->currentTarget = $this->setTarget($this->targets[0]);
                break;
                case '2':
                    $this->currentTarget = $this->setTarget($this->targets[1]);
                break;
            } 
            return 
                $this->currentTarget;
        }
    
    }
    $foo = new Test;
    
    echo $foo->set(1);
    echo $foo->set(2);
    echo $foo->set(1);
    echo $foo->set(1);
    echo $foo->set(2);
    echo $foo->set(1);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)