PHP:链接方法调用

发布于 2025-01-07 15:33:44 字数 889 浏览 2 评论 0原文

可能的重复:
PHP 方法链接?

我偶尔会看到一些 php 应用程序使用这样的类:

$Obj = new Obj();
$Obj->selectFile('datafile.ext')->getDATA();

上面的示例获取以下内容然后所选文件返回它们(仅是一个示例);

好吧,在我决定问你如何做到这一点之前,我尝试过:

class Someclass {
    public function selectFile( $filename  ) { 
       $callAclass = new AnotherClass( $filename );
       return $callAclass;
    }

    // Other functions ...
}

class AnotherClass {
    protected $file_name;

    public function __construct ( $filename ) { $this->file_name = $filename; }
    public function getDATA ( ) {
        $data = file_get_contents( $this->file_name );
        return $data;

    } 

    // funcs , funcs, funcs ....

}

这是完成这项任务的正确方法吗?请告诉我这些类的名称。

Possible Duplicate:
PHP method chaining?

I occasionally see some php applications use classes like that:

$Obj = new Obj();
$Obj->selectFile('datafile.ext')->getDATA();

The example above gets the contents of the selected file then returns them ( just an example );

Well, before i decided to ask you how can I do this, I tried this:

class Someclass {
    public function selectFile( $filename  ) { 
       $callAclass = new AnotherClass( $filename );
       return $callAclass;
    }

    // Other functions ...
}

class AnotherClass {
    protected $file_name;

    public function __construct ( $filename ) { $this->file_name = $filename; }
    public function getDATA ( ) {
        $data = file_get_contents( $this->file_name );
        return $data;

    } 

    // funcs , funcs, funcs ....

}

Is that the right way to accomplish this task? And please tell me what these classes are called.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

叶落知秋 2025-01-14 15:33:44

这称为方法链。看看这个问题

您可以通过以下方式实现您想要实现的目标:

class File 
{
    protected $_fileName;

    public function selectFile($filename) 
    { 
        $this->_fileName = $filename; 
        return $this;
    }

    public function getData()
    {
        return file_get_contents($this->_fileName);
    }
}


$file = new File();
echo $file->selectFile('hello.txt')->getData();

请注意,我们在 selectFile 中返回 $this,这使我们能够将另一个方法链接到它上面。

It's called method chaining. Have a look at this question on SO.

Here's a way you can do what you're trying to achieve:

class File 
{
    protected $_fileName;

    public function selectFile($filename) 
    { 
        $this->_fileName = $filename; 
        return $this;
    }

    public function getData()
    {
        return file_get_contents($this->_fileName);
    }
}


$file = new File();
echo $file->selectFile('hello.txt')->getData();

Notice we return $this in the selectFile, this enables us to chain another method onto it.

沦落红尘 2025-01-14 15:33:44

上面的例子(第一个)就是所谓的链接。这是一个常规类:

class a_class
{

    public function method_a()
    {
        echo 'method a';
    }

    public function method_b()
    {
        echo ' - method b';
    }

}

您可以这样称呼它们:

$class = new a_class();

$class->method_a();
$class->method_b();

这将回显“方法 a - 方法 b”。现在要链接它们,您将在方法中返回对象:

class a_class
{

    public function method_a()
    {
        echo 'method a';
        return $this;
    }

    public function method_b()
    {
        echo ' - method b';
        return $this;
    }

}

您将像这样调用它们:

$class = new a_class();

$class->method_a()->method_b();

这也会回显“方法 a - 方法 b”。

尽管我在最后一个方法中返回了对象 - 您严格意义上不需要这样做,只有前面的方法需要它来进行链接。

The above example (the first one) is something called chaining. Here's a regular class:

class a_class
{

    public function method_a()
    {
        echo 'method a';
    }

    public function method_b()
    {
        echo ' - method b';
    }

}

You'd call those like this:

$class = new a_class();

$class->method_a();
$class->method_b();

Which would echo 'method a - method b'. Now to chain them, you'd return the object in the methods:

class a_class
{

    public function method_a()
    {
        echo 'method a';
        return $this;
    }

    public function method_b()
    {
        echo ' - method b';
        return $this;
    }

}

You'd call those like this:

$class = new a_class();

$class->method_a()->method_b();

Which would also echo 'method a - method b'.

Although I have returned the object in the last method - you strictly wouldn't need to, only the preceding methods require that for chaining.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文