Solved

value, (stream and resource)

Posted on 2013-12-06
2
262 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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

867 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now