0

I was started my learing in OOP and I make a class but I have a problem now. This is my little code:

class User {

    public $name;
    public $age;
    public $city;

    public function setDate($setName, $setAge, $setCity) 
    {
        $this->name = $setName;
        $this->age = $setAge;
        $this->city = $setCity;
    }

    public function pullDate()
    {
        return $this->name;
        return $this->age;
        return $this->city;
    }

}

$newUser = new User;
$newUser->setDate('Adrian', '23', 'Poland');
echo $newUser->pullDate();

And when I get variable by echo $newUser->pullDate(); in response recerive only 'Adrian' ... How I can fix it?

Thanks for help!

3
  • 2
    Please edit your question and include tag for language you are using. Commented Dec 22, 2016 at 11:24
  • It's not clear what language this is - but I would assume the first return statement returns from the function so never gets to the other two data items you want Commented Dec 22, 2016 at 11:27
  • You can not return more than one object or value!, instead of return multiple create an object of class and return it as object. Commented Dec 22, 2016 at 11:33

1 Answer 1

1

A function can only return a single value. Placing multiple return statements in a row does not make any sense, the function will always return after the first such statement. Instead you have to combine the values, for example by creating an array out of them.

Take a look at this modified version, assuming that you are using php:

<?php 
class User {

    public $name;
    public $age;
    public $city;

    public function setDate($setName, $setAge, $setCity) 
    {
        $this->name = $setName;
        $this->age = $setAge;
        $this->city = $setCity;
    }

    public function pullDate() {
        return [
            'name' => $this->name,
            'age' => $this->age,
            'city' => $this->city
        ];
    }
}

$newUser = new User;
$newUser->setDate('Adrian', '23', 'Poland');
$userValues = $newUser->pullDate();
echo sprintf(
    "Name: %s, Age: %s, City: %s\n",
    $userValues['name'],
    $userValues['age'],
    $userValues['city']
);

An alternative would be to implement separate "getters" for each property:

class User {
    // ...
    public function getName() {
        return $this->name;
    }
    public function getAge() {
        return $this->age;
    }
    public function getCity() {
        return $this->city;
    }
    // ...
}

And then get the attributes one by one, just as desired, for example by doing:

echo sprintf(
    "Name: %s, Age: %s, City: %s\n",
    $newUser->getName(),
    $newUser->getAge(),
    $newUser->getCity()
);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for help! Previously i wrote getName, getAge, getCity but I thought it would be better to do it in one function.
As demonstrated, both is possible. Classical "getters" are a well known pattern which gives a lotof freedom especially for later modifications. But sometimes an (additional) combined method comes in handy too ;-)

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.