PHP   发布时间:2022-04-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了php-从类中直接使用多个函数大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

我如何从对象内联使用一个以上的函数
我有简单的课程:

class test
{
    private $string;

    function text($text)
    {
        $this->string = $text;
    }

    function add($text)
    {
        $this->string .= ' ' . $text;
    }
}

因此,我如何使用此类:

$class = new test();
$class->text('test')->add('test_add_1')->add('test_add_2');

不喜欢:

$class = new test();
$class->text('test')
$class->add('test_add_1')
$class->add('test_add_2')

在$string类的末尾将是:test test_add_1 test_add_2

解决方法:

您返回$this,然后可以继续处理该对象:

class test
{
    private $string;

    function text($text)
    {
        $this->string = $text;
        return $this;
    }

    function add($text)
    {
        $this->string .= ' ' . $text;
        return $this;
    }
}

大佬总结

以上是大佬教程为你收集整理的php-从类中直接使用多个函数全部内容,希望文章能够帮你解决php-从类中直接使用多个函数所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: