Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 一个动作有多个功能?_Php_File_Function - Fatal编程技术网

Php 一个动作有多个功能?

Php 一个动作有多个功能?,php,file,function,Php,File,Function,我目前在PHP中有以下代码: public function newFile($folder, $file){ fopen($folder."/".$file, 'w'); } 我是这样使用的: newFile('myfolder', 'myfile.txt'); newFile('myfile.txt') inFolder('myfolder'); 它工作得很好,但我想知道是否有可能创建一个可以像这样使用的函数: newFile('myfolder', 'myfile.txt')

我目前在PHP中有以下代码:

public function newFile($folder, $file){
    fopen($folder."/".$file, 'w');
}
我是这样使用的:

newFile('myfolder', 'myfile.txt');
newFile('myfile.txt') inFolder('myfolder');
它工作得很好,但我想知道是否有可能创建一个可以像这样使用的函数:

newFile('myfolder', 'myfile.txt');
newFile('myfile.txt') inFolder('myfolder');
如果可能的话,我该怎么做

我也可以用这个:

newFile('myfile.txt')->inFolder('myfolder');

我可以看出您正在尝试实现类似Objective-C和类似语言的语法,但不幸的是,您不能。只要习惯PHP语法就行了

不过,您可以使用数组获取命名参数:

function newFile($params){
    fopen($params['folder']."/".$params['file'], 'w');
}

newFile(array(
    'folder' => 'myfolder',
    'file' => 'myfile.txt'
));
或者,您可以使用代理来获得一种语法,如:newFile…->inFolder…,但这肯定是一种过度使用。

至于newFile'file'->inFolder'folder'的解决方案,这是可能的,但在这种情况下肯定是过度使用了。不过,下面是一个示例:

<?php
    class File_To_Be_Created {
        private $file;

        public function __construct($file) {
            $this->file = $file;
        }

        public function inFolder($folder) {
            $handle = fopen($folder . '/' . $this->file, 'w');

            if ($handle !== false) {
                fclose($handle);
            }
        }
    }

    function newFile($file) {
        return new File_To_Be_Created($file);
    }

    newFile('myfile.txt')->inFolder('myfolder');
?>

下面是一个您可能实现的示例。当您需要调用同一对象的函数lof时,通常会使用这种技术,以提高可读性

关键是,您的函数可能会返回对同一对象的引用:

class Creator {
  private $file ;
  private $folder = "" ;

  public function newFile($file){
    $this->file = $file ;
    return $this ;
  }

  public function inFolder($folder){
    $this->folder = $folder ;
    return $this ;
  }

  public function create(){
    return fopen($this->folder."/".$this->file, 'w');
  }
}

$creator = new Creator();

$creator
  ->newFile("test.txt")
  ->inFolder("test")
  ->create();

你在问什么?函数'param'函数'param'不是有效的PHP代码。函数“param”、“param”有什么问题?是的,请尝试更清楚地表达您想要实现的目标…我认为您正在寻找类似命名参数的习惯用法。如果能够做到这一点对您很重要,你可以使用如下语法:newFile'myfile.txt'->inFolder'myfolder'@Tim我怎么做?@Jeffrey我怎么做newFile->inFolder@Me123,跟着其他答案。但我强烈建议你不要那样做。这更复杂,我同意你的看法。甚至这里的数组技巧也不是惯用的。@Me123您的客户端真的想使用一个复杂、晦涩而且很可能比一个简单而优雅的解决方案消耗更多资源的解决方案吗?我倾向于告诉他们他们是笨蛋:我认为在全局命名空间中使用helper函数,这种事情会更漂亮。i、 newFile是一个免费函数,它返回代理的一个实例。这将是我选择的路径,而不是下面的其他版本。这对我来说更具可读性/可维护性。虽然可能是2的混合,使用构造函数在new上设置输入,而不是像下面那样在construct上设置一个,另一个作为另一个方法的参数