?
Solved

value, (stream and resource)

Posted on 2013-12-06
2
Medium Priority
?
270 Views
Last Modified: 2013-12-06
<?php
class Animal{
  public function __construct(){
    $this->created=time();
    $this->logfile_handle=fopen('log.txt','w');
  }
  public function __destruct(){
    fclose($this->logfile_handle);
  }
}

$c=new Animal();

Open in new window


Running this code in a debugger,
in the constructor
$this->logfile value=2, type=stream
in the destructor
$this->logfile value=2, type=resource

why value =2
what is the difference between stream and resource
0
Comment
Question by:rgb192
2 Comments
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 39701098
I'm guessing that you mean $this->logfile_handle and not $this->logfile?  Othewise we may not be looking at the right code sample.

From PHP.net:
fopen() binds a named resource, specified by filename, to a stream.

Worth reading:
http://php.net/manual/en/intro.stream.php
http://php.net/manual/en/resource.php

It's not worth overanalyzing this.  Just accept the fact that it works this way.  fopen() is called to open the file.  The function returns a resource, also called a handle, that can be used with other functions to read and write.  By naming the resource in the other function calls, your script can simultaneously read and update several files at a time.  When your logic indicates that the data access is complete, you want to use fclose().  I would avoid re-using a variable name that was a file resource; it's too easy to confuse yourself with something like that.
0
 

Author Closing Comment

by:rgb192
ID: 39701361
I think I fully understand but your advice is to accept and move on.
Thanks.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month15 days, 2 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question