简体   繁体   English

在PHP OOP中需要帮助

[英]Need Help in PHP OOP

I'm new in PHP OOP. 我是PHP OOP的新手。 I need some help for how to write the php OOP class then can call the class like below. 我需要一些有关如何编写php OOP类的帮助,然后可以像下面这样调用该类。

Query->table('user')->column('id','name')->where('name LIKE ?', 
  ["name"=> 'John'])->orderby('name', 'desc');

But, I had try so many time, what I can get it something like below then stop. 但是,我尝试了很多次,可以得到如下结果然后停止。

Query->table('user')->column('id','name');

I'm running out of ideas and I had google a lot, but fail to find any solution. 我的想法不多了,我有很多Google,但是找不到任何解决方案。

Any suitable help is very much appreciated. 非常感谢任何合适的帮助。

You just need to return on each function of class object itself using $this . 您只需要使用$this返回类对象本身的每个函数。 All functions that you want to run "in chain" should be public . 您要“在链中”运行的所有功能应该是public Check this code: 检查此代码:

<?php

class ClassName
{
    public function a()
    {
        // ...

        return $this;
    }

    public function b($param)
    {
        // ...

        return $this;
    }

    public function c()
    {
        // ...

        return $this;
    }

}

// testing
$obj = new ClassName;

$result = $obj->a()->b('someParam')->c();

You should read about method chaining . 您应该阅读方法链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM