只有在PHP代码中调用特定方法时才创建文件?

How to create file only is specific method is called in the code.

suppose say,

<?php

function foo(){
}
function foo1() {
}

?>

I want to create a file only if method foo1() was called. I dont want to create the file from constructor because this code might run without foo1() not being called(will unnecessarily create file). Also i might call foo1() multiple times in a single run but file should be created only once and I will add date into file.

Hope I am clear. How to do that?

Put your creation code in foo1(), you could then either ask the file if it already exists or keep your own boolean flag. I would prefer the first option.

Check the existence of the file before create it.

function foo1() {
    $path = '/foo/bar/my_file.txt';

    if (!file_exists($path)) {
        // Create your file here
    }

}

http://php.net/manual/en/function.file-exists.php