PhP foreach through class instances












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.










share|improve this question

























  • Can you please add var_dump or print_r on the $customer in your for-loop?

    – David Winder
    Oct 31 '18 at 15:42






  • 5





    You are returning INSIDE a foreach loop. So that loop will run only once

    – RiggsFolly
    Oct 31 '18 at 15:43








  • 5





    And you are returning an Array and NOT an Object

    – RiggsFolly
    Oct 31 '18 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.

    – iainn
    Oct 31 '18 at 15:44








  • 1





    You are STILL returning INSIDE the foreach loop and therefore only ONE Customer will be returned

    – RiggsFolly
    Oct 31 '18 at 15:55


















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.










share|improve this question

























  • Can you please add var_dump or print_r on the $customer in your for-loop?

    – David Winder
    Oct 31 '18 at 15:42






  • 5





    You are returning INSIDE a foreach loop. So that loop will run only once

    – RiggsFolly
    Oct 31 '18 at 15:43








  • 5





    And you are returning an Array and NOT an Object

    – RiggsFolly
    Oct 31 '18 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.

    – iainn
    Oct 31 '18 at 15:44








  • 1





    You are STILL returning INSIDE the foreach loop and therefore only ONE Customer will be returned

    – RiggsFolly
    Oct 31 '18 at 15:55
















0












0








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.










share|improve this question
















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.







php object foreach






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 9:15









Cœur

17.6k9105145




17.6k9105145










asked Oct 31 '18 at 15:40









FileInputStreamFileInputStream

4119




4119













  • Can you please add var_dump or print_r on the $customer in your for-loop?

    – David Winder
    Oct 31 '18 at 15:42






  • 5





    You are returning INSIDE a foreach loop. So that loop will run only once

    – RiggsFolly
    Oct 31 '18 at 15:43








  • 5





    And you are returning an Array and NOT an Object

    – RiggsFolly
    Oct 31 '18 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.

    – iainn
    Oct 31 '18 at 15:44








  • 1





    You are STILL returning INSIDE the foreach loop and therefore only ONE Customer will be returned

    – RiggsFolly
    Oct 31 '18 at 15:55





















  • Can you please add var_dump or print_r on the $customer in your for-loop?

    – David Winder
    Oct 31 '18 at 15:42






  • 5





    You are returning INSIDE a foreach loop. So that loop will run only once

    – RiggsFolly
    Oct 31 '18 at 15:43








  • 5





    And you are returning an Array and NOT an Object

    – RiggsFolly
    Oct 31 '18 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.

    – iainn
    Oct 31 '18 at 15:44








  • 1





    You are STILL returning INSIDE the foreach loop and therefore only ONE Customer will be returned

    – RiggsFolly
    Oct 31 '18 at 15:55



















Can you please add var_dump or print_r on the $customer in your for-loop?

– David Winder
Oct 31 '18 at 15:42





Can you please add var_dump or print_r on the $customer in your for-loop?

– David Winder
Oct 31 '18 at 15:42




5




5





You are returning INSIDE a foreach loop. So that loop will run only once

– RiggsFolly
Oct 31 '18 at 15:43







You are returning INSIDE a foreach loop. So that loop will run only once

– RiggsFolly
Oct 31 '18 at 15:43






5




5





And you are returning an Array and NOT an Object

– RiggsFolly
Oct 31 '18 at 15:44





And you are returning an Array and NOT an Object

– RiggsFolly
Oct 31 '18 at 15:44




1




1





return array(Customer(... - are you also missing a new here? The code you've posted should just cause a syntax error.

– iainn
Oct 31 '18 at 15:44







return array(Customer(... - are you also missing a new here? The code you've posted should just cause a syntax error.

– iainn
Oct 31 '18 at 15:44






1




1





You are STILL returning INSIDE the foreach loop and therefore only ONE Customer will be returned

– RiggsFolly
Oct 31 '18 at 15:55







You are STILL returning INSIDE the foreach loop and therefore only ONE Customer will be returned

– RiggsFolly
Oct 31 '18 at 15:55














2 Answers
2






active

oldest

votes


















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();
}





share|improve this answer


























  • This solved my problem. @RiggsFolly This method is also returning inside the foreach loop and it is working.

    – FileInputStream
    Oct 31 '18 at 15:56






  • 1





    "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.

    – Patrick Q
    Oct 31 '18 at 15:58













  • Sorry for that, my mistake.

    – FileInputStream
    Oct 31 '18 at 16:43



















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.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53087203%2fphp-foreach-through-class-instances%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    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();
    }





    share|improve this answer


























    • This solved my problem. @RiggsFolly This method is also returning inside the foreach loop and it is working.

      – FileInputStream
      Oct 31 '18 at 15:56






    • 1





      "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.

      – Patrick Q
      Oct 31 '18 at 15:58













    • Sorry for that, my mistake.

      – FileInputStream
      Oct 31 '18 at 16:43
















    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();
    }





    share|improve this answer


























    • This solved my problem. @RiggsFolly This method is also returning inside the foreach loop and it is working.

      – FileInputStream
      Oct 31 '18 at 15:56






    • 1





      "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.

      – Patrick Q
      Oct 31 '18 at 15:58













    • Sorry for that, my mistake.

      – FileInputStream
      Oct 31 '18 at 16:43














    2












    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();
    }





    share|improve this answer















    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();
    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Oct 31 '18 at 16:02

























    answered Oct 31 '18 at 15:52









    David WinderDavid Winder

    4,1023827




    4,1023827













    • This solved my problem. @RiggsFolly This method is also returning inside the foreach loop and it is working.

      – FileInputStream
      Oct 31 '18 at 15:56






    • 1





      "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.

      – Patrick Q
      Oct 31 '18 at 15:58













    • Sorry for that, my mistake.

      – FileInputStream
      Oct 31 '18 at 16:43



















    • This solved my problem. @RiggsFolly This method is also returning inside the foreach loop and it is working.

      – FileInputStream
      Oct 31 '18 at 15:56






    • 1





      "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.

      – Patrick Q
      Oct 31 '18 at 15:58













    • Sorry for that, my mistake.

      – FileInputStream
      Oct 31 '18 at 16:43

















    This solved my problem. @RiggsFolly This method is also returning inside the foreach loop and it is working.

    – FileInputStream
    Oct 31 '18 at 15:56





    This solved my problem. @RiggsFolly This method is also returning inside the foreach loop and it is working.

    – FileInputStream
    Oct 31 '18 at 15:56




    1




    1





    "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.

    – Patrick Q
    Oct 31 '18 at 15:58







    "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.

    – Patrick Q
    Oct 31 '18 at 15:58















    Sorry for that, my mistake.

    – FileInputStream
    Oct 31 '18 at 16:43





    Sorry for that, my mistake.

    – FileInputStream
    Oct 31 '18 at 16:43













    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.






    share|improve this answer




























      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.






      share|improve this answer


























        1












        1








        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Oct 31 '18 at 15:56









        Jose RodriguezJose Rodriguez

        22716




        22716






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53087203%2fphp-foreach-through-class-instances%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            The Sandy Post

            Danny Elfman

            Pages that link to "Head v. Amoskeag Manufacturing Co."