PHP OOP嵌套方法?

时间:2013-03-22 15:35:44

标签: php function nested

我知道您可以在PHP中使用嵌套函数。

以下是问题:

我有这段代码:

    class ClassName
    {
        private $data;

        function __construct()
        {

        }

        public function myFunction()
        {
            if($condition1 != NULL)
            {
                if(!empty($condition2) && $this->data->condition3 == NULL)
                {

                    $this->do1($condition2);
                    $this->do3($condition2);
                    $this->do3($condition2);
                    $this->doBLAH();

                }
                elseif(empty($checkoutItem->rebillCheckoutItemId))
                {
                    $this->do1($condition2);
                    $this->do3($condition2);
                    $this->do3($condition2);
                    $this->doSomethingElse();

                }
            }               

        }

    }

如您所见,这部分是多余的:

$this->do1($condition2);
$this->do3($condition2);
$this->do3($condition2);

所以摆脱冗余我可以创建一个新方法:

    private function doSomething($condition2)
    {
        $this->do1($condition2);
        $this->do3($condition2);
        $this->do3($condition2);        
    }

但我不想。它可能会使我的代码变得混乱。

只是想知道是否有可能做类似的事情:

        public function myFunction()
        {
            if($condition1 != NULL)
            {
                if(!empty($condition2) && $this->data->condition3 == NULL)
                {

                    $this->doSomething($condition2);
                    $this->doBLAH();

                }
                elseif(empty($checkoutItem->rebillCheckoutItemId))
                {
                    $this->doSomething($condition2);
                    $this->doSomethingElse();

                }
            }
            function doSomething($condition2)
            {
                $this->do1($condition2);
                $this->do3($condition2);
                $this->do3($condition2);        
            }               

        }

我尝试了但它抛出Fatal error: Call to undefined function doSomething()。有什么窍门吗?我做错了吗?

更新

我在YII框架中工作,它给了我一个例外:BlahWidget and its behaviors do not have a method or closure named "doSomething"

2 个答案:

答案 0 :(得分:2)

您可以在本地范围内定义匿名函数。否则,该函数仍将在全局范围或命名空间中定义。

这应该从PHP 5.4开始,因为$ this在闭包内使用。在5.3中,你需要做额外的体操。见下文。

    public function myFunction()
    {
        $do = function ($condition2) {
            $this->do1($condition2);
            $this->do3($condition2);
            $this->do3($condition2);        
        };

        if($condition1 != NULL)
        {
            if(!empty($condition2) && $this->data->condition3 == NULL)
            {

                $do($condition2);
                $this->doBLAH();

            }
            elseif(empty($checkoutItem->rebillCheckoutItemId))
            {
                $do($condition2);
                $this->doSomethingElse();

            }
        }               

    }

现在是5.3版本。请注意,调用的函数需要公开。

    public function myFunction()
    {
        $that = $this;
        $do = function ($condition2) use ($that) {
            $that->do1($condition2);
            $that->do3($condition2);
            $that->do3($condition2);        
        };

        if($condition1 != NULL)
        {
            if(!empty($condition2) && $this->data->condition3 == NULL)
            {

                $do($condition2);
                $this->doBLAH();

            }
            elseif(empty($checkoutItem->rebillCheckoutItemId))
            {
                $do($condition2);
                $this->doSomethingElse();

            }
        }               

    }

在这样的例子中,对于5.3,我宁愿在类上创建一个私有方法。

答案 1 :(得分:0)

首先,你不能拥有一个名为do()的功能。它是reserved

让我们将您的do()更改为do_somthing()。然后,您需要使用$this->do_something()在任何您想要使用它的地方调用它。

相关问题