Home Backend Development PHP Tutorial Problem with working directory in destructor in php

Problem with working directory in destructor in php

Jul 25, 2016 am 09:07 AM

  1. <?php
  2. class Test {
  3. public function __construct(){
  4. $this->_log('start');
  5. }
  6. public function __destruct () {
  7. $this-> _log('finish');
  8. }
  9. public function _log ($str) {
  10. <span style="white-space:pre"> </span>error_log($str . "n", 3, './log.log');
  11. }
  12. }
  13. $test = new Test;
  14. ?>
Copy the code

and found that there is only start in log.log and no finish.

After clearing the contents in the log, modify the program: unset($test); Sure enough, there was a start and a finish.

It seems that __destruct will be executed only after unset.

But the matter is not over yet. I modified the program again, changed the __destruct function, and deleted the unset code:

  1. <?php
  2. public function __destruct () {
  3. echo 'finish';
  4. $this->_log('finish');
  5. }
  6. ?>
Copy code

At this time, 'finish' was actually printed on the screen, which proved that the destructor was indeed executed.

After being puzzled, I started looking for information on the Internet and saw that error_log was used in the destructor of a friend and was executed. The difference from my usage is that he did not specify the error_log file, but set it to the default log file, so I simulated it and modified my program:

  1. <?php
  2. public function _log ($str) {
  3. error_log($str . "n");
  4. error_log($str . "n", 3, './log.log') ;
  5. }
  6. ?>
Copy the code

I found that the log.log still started after start, but there was no finish. And E:AppServApache2.2logserror.log (the default log file of my apache configuration) really has start and finish. It was a strange question and I couldn’t figure it out, so I went to the group to ask. My friends in the group either asked me to check for syntax errors or to check file permissions. I made sure there were no problems with these two. I said, "There is no permission problem in the Windows system. It is definitely not caused by this." A group friend said: "windos system, no explanation!" I said: "This must have nothing to do with the system. I suspect it is an apache problem." So I restored the _log and ran it on the linux+nginx virtual machine.

  1. <?php
  2. public function _log ($str) {
  3. error_log($str . "n", 3, './log.log');
  4. }
  5. ?>
Copy the code

log.log and start and finish are successfully written.

This makes me doubt my judgment even more: it’s caused by apache

So I struggled for an hour, installed apache on the server and ran the file. Still the same result, finish was not successfully written.

At this time, a colleague told me, try writing an absolute path, so I modified another _log

  1. <?php
  2. public function _log ($str) {
  3. error_log($str . "n", 3, '/var/www/apache/log.log');
  4. }
  5. ?> ;
Copy the code

Look at it again and write "finish" successfully. I'm so excited. I finally found the problem. I modified the program again.

  1. <?php
  2. public function __construct(){
  3. echo getcwd(). ;br />';
  4. }
  5. ?>
  6. Copy the code
to view the running results: /var/www/apache / Run it under the nginx server and get the result: /var/www/apache /var/www/apache

It turns out that the destructor of apache will change the directory of the current program, so if you use a relative directory, the corresponding file will not be found, and the writing will of course fail. Learning a language is not easy, it pays to practice more.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Introduction to the Instagram API

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

Notifications in Laravel

See all articles