1

Trying to figure out how to work with array elements with a public function inside a PHP class. Have reviewed already similar questions, but without being able to resolve the issue. Below is what I have so far.

class myClass
{
    public $inputNumber = 27;
    public $inputArray = array(1, 2, 4);
    public $outputArray = array($inputArray[0]*$inputNumber, $inputArray[1]*$inputNumber, $inputArray[2]*$inputNumber);

    public function printOutput()
    {
        return "1st value is " . $this->outputArray[0] . "<br>";
        return "2nd value is " . $this->outputArray[1] . "<br>";
        return "3rd value is " . $this->outputArray[2] . "<br>";
    }
}

$obj = new myClass;

echo $obj->printOutput();
3
  • 1
    You can only return once within a function, so that's not going to work. If you will need to return $this->outputArray, then access the values in the calling element (or simply echo or print instead of return, if this suits) Commented Oct 8, 2017 at 15:10
  • How about assigning the values in the constructor: function __construct(){ $this->outputArray = array(...);} and remove the 2nd and 3rd return. Concenate the strings and return only once. Commented Oct 8, 2017 at 15:33
  • And you are missing the references (this->). Commented Oct 8, 2017 at 15:45

2 Answers 2

1

You can also use defined values. other wise good to pass in constructor

<?php

class myClass
{
public $inputNumber = 27;
public $inputArray = array(1, 2, 4);
public $outputArray = array();

function __construct() {
   $this->outputArray= array($this->inputArray[0]*$this->inputNumber, 
                             $this->inputArray[1]*$this->inputNumber, 
                             $this->inputArray[2]*$this->inputNumber
                             );
}
public function printOutput()
{
    $output = "1st value is " . $this->outputArray[0] . "<br>";
    $output .= "2nd value is " . $this->outputArray[1] . "<br>";
    $output .= "3rd value is " . $this->outputArray[2] . "<br>";
    return $output;
 }
}

$obj = new myClass;
echo $obj->printOutput();

here is running snippet: https://ideone.com/miOQJB

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, Ankur. You've provided something I can work with.
0
class myClass
{
    public $inputNumber = (int) 27;
    public $inputArray = array();
    public $outputArray = array();

   function __construct($inputNumber = 27, $inputArray = array(1,2,4)) {
       $this->inputNumber = (int) $inputNumber;
       $this->inputArray = (array) $inputArray;
       $this->outputArray= array($this->inputArray[0]*$this->inputNumber, $this->inputArray[1]*$this->inputNumber, $this->inputArray[2]*$this->inputNumber);
    }

    public function printOutput()
    {
        return "1st value is " . $this->outputArray[0] . "<br>"
        . "2nd value is " . $this->outputArray[1] . "<br>"
        . "3rd value is " . $this->outputArray[2] . "<br>";
    }
}

$obj = new myClass;

echo $obj->printOutput();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.