Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Sep 2012
    Posts
    76
    Thanks
    61
    Thanked 0 Times in 0 Posts

    Pass the results of a query to another method

    Hello, I'm after some help if that is possible please. I have a query that I pass into my constructor like so

    PHP Code:
    class myClass {
      public function 
    __construct(PDO $c,$query) {
        
    $sql $c->query($query);
        
    $sql->setFetchMode(PDO::FETCH_ASSOC);
         while(
    $row$sql->fetch()) {
           
    $this->results_set[] = $row;
         }
         return 
    $this->results_set;
      }
      public function 
    createFile() { //Want to do something with the results from the query in here }

    PHP Code:
    $a = new myClass ($c,"SELECT id FROM table");
    $a->createFile(); 
    But my question is how do I pass and use the results from the constructor in the createFile() method. Thank you.
    Last edited by Oatley; 05-08-2013 at 03:22 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    You don't. You don't even need the return; constructors are void return, so you construct an object with new, and the constructor is then invoked if available. The only time you won't get an object back is if you throw an Exception within the constructor or if the constructor is private.
    In the createFile method, you can access the results using $this->results_set. You should also add private $result_set; to the class as a member property; without it you use php's "automagical" functionality that creates the public property by itself which is of course a debugging nightmare.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    Oatley (05-08-2013)

  • #3
    New Coder
    Join Date
    Sep 2012
    Posts
    76
    Thanks
    61
    Thanked 0 Times in 0 Posts
    Thank you. That's really helped me. So just in terms of visibility, something like this?

    PHP Code:
    class myClass {
     private 
    $results_set = array();
       public function 
    __construct(PDO $c,$query) {
         
    $sql $c->query($query);
         
    $sql->setFetchMode(PDO::FETCH_ASSOC);
           while(
    $row$SQL->fetch()) { 
            
    $this->results_set[] = $row;
            
    $results_set $this->results_set[];
        }

    Thank you very much.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Thats correct. That will prevent an instance of myClass from accessing $obj->results_set outside of scope from the class.
    Zend really did take a bizarre approach to the objects in the 5.x core. Stacking a datatype weak language on top of an automagically property creation is *really* a bad idea, although it is possible that its a limitation from the implicitly extended stdclass object which only works with automagical properties (but I kinda doubt that limitation is real).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    Oatley (05-09-2013)

  • #5
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,253
    Thanks
    12
    Thanked 341 Times in 337 Posts
    if you want to make use of PDO’s automagical looping, you don’t even need the while() loop:
    PHP Code:
    class myClass {
     private 
    $results_set = array();
       public function 
    __construct(PDO $c,$query) {
         
    $sql $c->query($query);
         
    $sql->setFetchMode(PDO::FETCH_ASSOC);
         
    $this->results_set $sql;

    later you can simply loop using foreach:
    PHP Code:
    public function someMethod($name) {
      
    $str '';
      foreach (
    $this->results_set as $row) {
        
    $str .= $row[$name];
      }
      return 
    $str;

    only if you need to loop multiple times over the result set the conversion to an array is needed, which doesn’t need a loop either:
    PHP Code:
    class myClass {
     private 
    $results_set = array();
       public function 
    __construct(PDO $c,$query) {
         
    $sql $c->query($query);
         
    $sql->setFetchMode(PDO::FETCH_ASSOC);
         
    $this->results_set $sql->fetchAll();

    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •