Solved

value, (stream and resource)

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

705 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

17 Experts available now in Live!

Get 1:1 Help Now