Solved

value, (stream and resource)

Posted on 2013-12-06
2
264 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 109

Accepted Solution

by:
Ray Paseur earned 500 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

776 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