0

I'm wondering about this weird thing:

public function getAllCustomers()
{
    $customers = $this->redis->keys("customer:*");
    foreach ($customers as $value) {
        return new \Customer($this->redis->hget($value,"name"),$this->redis->hget($value,"id"),$this->redis->hget($value,"email"));
    }
}

This method returns all customers from my database.

But if I try to loop through all of these customers:

foreach ($customerController->getAllCustomers() as $customer) {
    var_dump($customer);
}

The getName() method is not found. var_dump returns:

NULL
NULL
NULL

Customer class:

class Customer {
    var $name;
    var $id;
    var $email;

    function __construct($name, $id,$email) {
        $this->name = $name;
        $this->id = $id;
        $this->email = $email;
    }

     /**
     * @return mixed
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @return mixed
    */
    public function getEmail()
    {
        return $this->email;
    }

    public function __toString()
    {
       return "";
    }
}

I'm pretty new to PHP and don't understand why I can't access the Customer object's field.

14
  • Can you please add var_dump or print_r on the $customer in your for-loop? Commented Oct 31, 2018 at 15:42
  • 5
    You are returning INSIDE a foreach loop. So that loop will run only once Commented Oct 31, 2018 at 15:43
  • 5
    And you are returning an Array and NOT an Object Commented Oct 31, 2018 at 15:44
  • 1
    return array(\Customer(... - are you also missing a new here? The code you've posted should just cause a syntax error. Commented Oct 31, 2018 at 15:44
  • 1
    You are STILL returning INSIDE the foreach loop and therefore only ONE Customer will be returned Commented Oct 31, 2018 at 15:55

2 Answers 2

2

Your Problem: you do not return array of customer but only one. You getting null because your function return only 1 object -> and in PHP, when using foreach loop on object you get his fields -> and the fields do not have the getName function.

Solution: Init customer array, populate it and return from the function.

 public function getAllCustomers()
{
    $customers = $this->redis->keys("customer:*");
    $customersObjs = array();
    foreach ($customers as $value) {
        $customersObjs[] = new Customer($this->redis->hget($value,"name"),$this->redis->hget($value,"id"),$this->redis->hget($value,"email")));
    }
    return $customersObjs;
}

Now you have array of the customersObjs you can loop on with:

foreach ($customerController->getAllCustomers() as $customer) {
    echo $customer->getName();
}
Sign up to request clarification or add additional context in comments.

3 Comments

This solved my problem. @RiggsFolly This method is also returning inside the foreach loop and it is working.
"is also returning inside the foreach loop" No, it definitely is not. Please take the time to review the difference between this and what you had. You might also want to review the documentation for what exactly return does.
Sorry for that, my mistake.
1

Solution :

public function getAllCustomers()
{
    $customers = $this->redis->keys("customer:*");
    $custumersArray = array();
    foreach ($customers as $value) {
        $custumersArray[] = \Customer($this->redis->hget($value,"name"),$this->redis->hget($value,"email"),$this->redis->hget($value,"id"));
    }
    return $custumersArray;
}

the problem was that you are returning a single array but not a array list.

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.