如何在 PHP5 中构建多 oop 函数

发布于 2024-09-11 10:17:09 字数 206 浏览 7 评论 0原文

我有一个关于 PHP5 中的 OOP 的问题。我看到越来越多的代码是这样写的:

$object->function()->first(array('str','str','str'))->second(array(1,2,3,4,5));

但我不知道如何创建这个方法。我希望有人能在这里帮助我,:0) 非常感谢。

I have a question about OOP in PHP5. I have seen more and more code written like this:

$object->function()->first(array('str','str','str'))->second(array(1,2,3,4,5));

But I don't know how to create this method. I hope somebody can help me here, :0) thanks a lot.

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

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

发布评论

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

评论(2

恋竹姑娘 2024-09-18 10:17:09

在您自己的类中链接此类方法的关键是返回一个对象(几乎总是 $this),然后将该对象用作下一个方法调用的对象。

就像这样:

class example
{
    public function a_function()
    {
         return $this;
    }

    public function first($some_array)
    {
         // do some stuff with $some_array, then...
         return $this;
    }
    public function second($some_other_array)
    {
         // do some stuff
         return $this;
    }
}

$obj = new example();
$obj->a_function()->first(array('str', 'str', 'str'))->second(array(1, 2, 3, 4, 5));

注意,可以返回 $this 以外的对象,上面的链接实际上只是表示 $a = $obj->first(. ..); $b = $a->second(...);,减去设置调用后永远不会再使用的变量的丑陋。

The key to chaining methods like that within your own classes is to return an object (almost always $this), which then gets used as the object for the next method call.

Like so:

class example
{
    public function a_function()
    {
         return $this;
    }

    public function first($some_array)
    {
         // do some stuff with $some_array, then...
         return $this;
    }
    public function second($some_other_array)
    {
         // do some stuff
         return $this;
    }
}

$obj = new example();
$obj->a_function()->first(array('str', 'str', 'str'))->second(array(1, 2, 3, 4, 5));

Note, it's possible to return an object other than $this, and the chaining stuff above is really just a shorter way to say $a = $obj->first(...); $b = $a->second(...);, minus the ugliness of setting variables you'll never use again after the call.

你好,陌生人 2024-09-18 10:17:09
$object->function()->first(array('str','str','str'))->secound(array(1,2,3,4,5));

这不是严格有效的 PHP,但这说明的是...您正在调用 $object 类上的方法,该方法本身返回一个对象,您在该对象中调用名为 first() 的方法它还返回一个对象,您在该对象中调用名为 second() 的方法。

因此,这不一定只是一个具有一种方法的类(尽管可能是),而是一系列可能不同的类。

像这样的东西:

class AnotherClass {
    public function AnotherClassMethod() {
        return 'Hello World';
    }
}

class MyClass {
    public function MyClassMethod() {
        return new AnotherClass();
    }
}

$object = new MyClass();
echo $object->MyClassMethod()->AnotherClassMethod();  // Hello World
$object->function()->first(array('str','str','str'))->secound(array(1,2,3,4,5));

This isn't strictly valid PHP, but what this is saying is... You are calling a method on the $object class that itself returns an object in which you are calling a method called first() which also returns an object in which you are calling a method called second().

So, this isn't necessarily just one class (although it could be) with one method, this is a whole series of possibly different classes.

Something like:

class AnotherClass {
    public function AnotherClassMethod() {
        return 'Hello World';
    }
}

class MyClass {
    public function MyClassMethod() {
        return new AnotherClass();
    }
}

$object = new MyClass();
echo $object->MyClassMethod()->AnotherClassMethod();  // Hello World
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文