Yamaha32088
Yamaha32088

Reputation: 4163

Get two values returned from class

I am trying to understand object oriented PHP programming and wrote a small class to learn. I am having trouble understanding why its not working the way I intend. I have two variables inside the class method hello() $result and $test. I am trying to access the data that is stored in those two variables and print it to the screen. I know I can just call an echo inside the method but I am trying to get it to echo outside of it.

What I get printed to the screen is 88 it does not print out the second variable $test. I am trying to understand why thats happening. My lack of understanding probably shows in the code.

<?php

class simpleClass{

    public function hello($result,$test) {
        $result = 4+4;
        $test = 10+5;
        return $result;
        return $test;
    }
}

$a = new simpleClass;
echo $a->hello();
echo $a->hello($result, $test);

?>

Upvotes: 0

Views: 86

Answers (4)

zamnuts
zamnuts

Reputation: 9592

The complicated answer is to use a model.

class simpleResultTestModel {
    public $result;
    public $test;
    public function __construct($result,$test) {
        $this->result = $result;
        $this->test = $test;
    }
}

class simpleClass {
    public function hello($result=4, $test=10) {
        $result = $result+4;
        $test = $test+5;
        return new simpleResultTestModel($result, $test);
    }
}

This way, you know simpleClass->hello() will always return an instance of simpleResultTestModel.

Also, I updated your hello method definition. You have two parameters, but don't actually apply them; I took the liberty of setting default values and then used them in the computation.

Usage:

$a = new simpleClass();

$first = $a->hello();
echo $first->result;
echo $first->test;

$second = $a->hello($first->result,$first->test);
echo $second->result;
echo $second->test;

I would try to stay away from passing by reference (especially within a class definition) unless you have a legitimate reason for doing so. It is bad practice when creating instances of classes (i.e. "sticky values" if you will).

Upvotes: 1

davidkonrad
davidkonrad

Reputation: 85528

Use parameter referencing :

class simpleClass{

    public function hello(&$result, &$test) {
        $result = 4+4;
        $test = 10+5;
    }
}

$a = new simpleClass;
$result=''; $test='';

$a->hello($result, $test);

echo $result;
echo '<br>';
echo $test;

8
15

To clarify, when you add & to a function param, the value of that param - if you change or manipulate it inside the function - is handled back to your original variable passed. So you dont even have to return a result, and lets say pack it into an array or stdObject and unpack it afterwards. But you can still return something from the function, eg

$ok = $a->hello($result, $test);

as a flag to indicate if the calculation went right, for instance.

Upvotes: 1

DevZer0
DevZer0

Reputation: 13535

you can return a list or array

public function hello($result,$test) {
        $result = 4+4;
        $test = 10+5;
        return array($result, $test);
    }

Upvotes: 2

Bad Wolf
Bad Wolf

Reputation: 8349

You cannot have multiple return statements in the same function because of the way return works. When a return statement is encountered the function stops executing there and then, passing back to the caller. The rest of the function never runs.

Upvotes: 1

Related Questions