How to work with file and streams in php,case: if we open file in Class A and pass open stream to Cl

Posted by Rachel on Stack Overflow See other posts from Stack Overflow or by Rachel
Published on 2010-03-30T14:50:18Z Indexed on 2010/03/30 14:53 UTC
Read the original article Hit count: 306

Filed under:
|
|

I have two class, one is Business Logic Class{BLO} and the other one is Data Access Class{DAO} and I have dependency in my BLO class to my Dao class.

Basically am opening a csv file to write into it in my BLO class using inside its constructor as I am creating an object of BLO and passing in file from command prompt:

Code:

$this->fin = fopen($file,'w+') or die('Cannot open file');

Now inside BLO I have one function notifiy, which call has dependency to DAO class and call getCurrentDBSnapshot function from the Dao and passes the open stream so that data gets populated into the stream.

Code: Blo Class Constructor:

public function __construct($file)
{
    //Open Unica File for parsing.
    $this->fin = fopen($file,'w+') or die('Cannot open file');
    // Initialize the Repository DAO.
    $this->dao = new Dao('REPO');
}

Blo Class method that interacts with Dao Method and call getCurrentDBSnapshot.

public function notifiy()
{
    $data = $this->fin;
    var_dump($data); //resource(9) of type (stream)
    $outputFile=$this->dao->getCurrentDBSnapshot($data); // So basically am passing in $data which is resource((9) of type (stream)
}

Dao function: getCurrentDBSnapshot which get current state of Database table.

public function getCurrentDBSnapshot($data)
{
    $query = "SELECT * FROM myTable";
    //Basically just preparing the query.
    $stmt = $this->connection->prepare($query);

    // Execute the statement
    $stmt->execute();

    $header = array();
    while ($row=$stmt->fetch(PDO::FETCH_ASSOC))
    {   
        if(empty($header))
        {
        // Get the header values from table(columnnames)
        $header = array_keys($row);
        // Populate csv file with header information from the mytable
        fputcsv($data, $header);
        }
    // Export every row to a file
    fputcsv($data, $row);
    }
    var_dump($data);//resource(9) of type (stream)
    return $data;
}

So basically in am getting back resource(9) of type (stream) from getCurrentDBSnapshot and am storing that stream into $outputFile in Blo class method notify.

Now I want to close the file which I opened for writing and so it should be fclose of $outputFile or $data, because in both the cases it gives me:

var_dump(fclose($outputFile)) as bool(false)
var_dump(fclose($data)) as bool(false)

and

var_dump($outputFile) as resource(9) of type (Unknown)
var_dump($data) as resource(9) of type (Unknown)

My question is that if I open file using fopen in class A and if I call class B method from Class A method and pass an open stream, in our case $data, than Class B would perform some work and return back and open stream and so

How can I close that open stream in Class A's method or it is ok to keep that stream open and not use fclose ?

Would appreciate inputs as am not very sure as how this can be implemented.

© Stack Overflow or respective owner

Related posts about php

Related posts about csv