Jump to content

Returning an object from a function


YourNameHere

Recommended Posts

Hi I am writing helper classes to help me code faster.

 

Right now I am working on a database class.

 

I want to be able to us function fetch_all() and have it return the result set.

here is what I have.

function query($query,$debug = '')
        {
            $result = mysql_query($query);
            if ($debug !== '')
            {
                    if ($result = mysql_query($query))
                {
                    echo "Query worked: ".$query.'<br>Returned: '.mysql_num_rows($result).' rows.<br>';
                } else
                {
                    echo "Query failed: ".$query.'<br>';
                    die(mysql_error());
                }
            } 
            $data = mysql_fetch_object($result);           
            return $data; 
        }
function fetch_all($table, $debug = '', $where = NULL)
        {
            if (is_array($table))
            {
                echo "Use $DB->join();"
            }
            if ($where !== NULL)
            {
                mysql_real_escape_string($where);
                $sql = "SELECT * FROM `".$table."` WHERE ".$where;
            } else
            {
                $sql = "SELECT * FROM ". $table;
            }
            $result = $this->query($sql, $debug);
            
            return $this;
        }

I included the query() function in the snippet because fetch_all() is dependant on it.

 

What I want to be able to do is:

 

$DB->fetch_all($query);

then have $data available to use the results that I fetched. right now var_dump($data) returns NULL

Link to comment
https://forums.phpfreaks.com/topic/198059-returning-an-object-from-a-function/
Share on other sites

Why would you want to update all records after fetching them anyway, wouldn't you just want an update_all function so you don't query the whole table just to do an update?  Plus wouldn't you want to query the table AFTER updating?

 

That's not important, that was just an example. I wasn't thinking of a real-world example of the usage of chaining. I just need $data (the result set from he query() function )to be passed to fetch_all() method so that I can return it and use it in:

$db->fetch_all();

$data is available to the rest of the script that fetch_all() is called from.

You would need to make $data a private property then access within both methods using $this->data.

 

As for your chaining idea, a method can only return one value.

 

Fatal error: Cannot access private property DB::$data

 

//db.php
class DB
    {
        private $data;
        function query....

//view.php
$db->fetch_all('kfm_users','', 'username = "cody"');
        var_dump($db->data);

 

I made it a public property and now the above code works! thanks guys!

$db->fetch_all() should return $this->data, then you won't need to make it public.

ok but it doesn't. I will include the code so you can see it and tell me if I am just not doing it the right way, but it seems I am doing it that way you say and am getting an error.

class DB
    {
        private $data;
        
        function query($query,$debug = '')
        {
            
            $result = mysql_query($query);
            if ($_SESSION['DB'] == 'closed')
            {
                echo 'The DB connection is closed';
            } 
            elseif
            ($debug !== '')
            {
                    if ($result = mysql_query($query))
                {
                    echo "Query worked: ".$query.'<br>Returned: '.mysql_num_rows($result).' rows.<br>';
                } else
                {
                    echo "Query failed: ".$query.'<br>';
                    die(mysql_error());
                }
            } 
            $this->data = mysql_fetch_array($result);
            
        }

// In the calling script

$db->connect($connection info)->fetch_all($query data);
        $db->close($link);
var_dump($db->data); 

 

That returns:

Fatal error:  Cannot access private property DB::$data in...

 

Side note: I'm going to refactor this class to implement the "singleton method" so to do out with the connect and close methods.

 

Your current implementation is terrible design.

This is just unnecessary.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.