Problems with working directories in destructors in PHP

Source: Internet
Author: User
Tags log log nginx server
    1. Class Test {
    2. Public Function __construct () {
    3. $this->_log (' Start ');
    4. }
    5. Public Function __destruct () {
    6. $this->_log (' Finish ');
    7. }
    8. Public Function _log ($STR) {
    9. error_log ($str. "\ n", 3, './log.log ');
    10. }
    11. }
    12. $test = new test;
    13. ?>
Copy Code

Found in Log.log only start, no finish.

After emptying the contents of the log, modify the program: unset ($test); Sure enough, with start, and finish.

It seems that it is really unset to execute __destruct.

But the thing is not over, I modified the program, changed the __destruct function, while deleting the Code of unset:

    1. Public Function __destruct () {
    2. Echo ' Finish ';
    3. $this->_log (' Finish ');
    4. }
    5. ?>
Copy Code

The screen actually prints out ' finish ', which proves that the destructor was actually executed.

Baffled after the solution, began to find information on the Internet, see a friend of the destructor used in the error_log and executed, and I use the difference is that he did not specify the Error_log file, but to the default log file, so I simulated a bit, and modified my program:

    1. Public Function _log ($STR) {
    2. Error_log ($str. "\ n");
    3. Error_log ($str. "\ n", 3, './log.log ');
    4. }
    5. ?>
Copy Code

I found Log.log still after start, no finish. and E:\AppServ\Apache2.2\logs\error.log (the default log log file for my Apache configuration) really has start and finish. Strange question, can't think of, so went to the group inside asked, the group of friends either let me check the syntax error, or check the file permissions, I am sure that these two do not have any problems. I said "Windows system, there is no permission problem, certainly not this caused" a group of friends said: "WinDOS system, do not explain! "I said," This certainly doesn't matter with the system, I suspect it's Apache's problem. "So I restored the _log, to the Linux+nginx of the virtual machine ran a bit."

    1. Public Function _log ($STR) {
    2. Error_log ($str. "\ n", 3, './log.log ');
    3. }
    4. ?>
Copy Code

Start and finish were successfully written in Log.log.

This makes me more skeptical of my judgment: Apache leads to

So it took one hours to install Apache on the server and run the file, or the same result, finish did not write successfully.

At this time a colleague told me that you write the absolute path to try, so I modified a _log

    1. Public Function _log ($STR) {
    2. Error_log ($str. "\ n", 3, '/var/www/apache/log.log ');
    3. }
    4. ?>
Copy Code

Look again, successfully write "Finish", excited Ah, finally found the problem, I modified the program

    1. Public Function __construct () {
    2. Echo GETCWD (). '
      ';
    3. }
    4. Public Function __destruct () {
    5. Echo GETCWD (). '
      ';
    6. }
    7. ?>
Copy Code

View running results:/var/www/apache/

Then to the Nginx server to run, get the result:/var/www/apache/var/www/apache

The original Apache destructor will change the current program's directory, so the relative directory, you can not find the corresponding file, write of course also failed. It's not easy to learn a language, it's good to practise more.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.