-1

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?

overFlow
  • 27
  • 2
  • You're overwriting your `$target` array with a string as soon as you call `setTarget`. Also, `setTarget` doesn't return anything so it doesn't assign any value to `$this->message` – Phil Jul 27 '17 at 00:59
  • When you first echo $foo->set(1); the protected target value is target[0] ,not your ini array – mmmorgen Jul 27 '17 at 01:02
  • What do you propose? How to repair? – overFlow Jul 27 '17 at 01:09

1 Answers1

0

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);
Michel
  • 950
  • 14
  • 23