【问题标题】:Phalcon library class calling a function within anotherPhalcon 库类调用另一个函数
【发布时间】:2015-04-28 17:19:29
【问题描述】:

我正在使用 phalcon 2.0.0,我试图在另一个函数中调用一个函数,但来自同一个类,如下所示,由于某种原因,我得到一个空白页。当我从第一个开始评论第二个函数的调用时,页面加载正确。

<?php

use Phalcon\Mvc\User\Component;

class Testhelper extends Component {

    public function f1($data) {
        $tmp = $this->f2($data);
        return $tmp;
    }

    public function f2($data) {
       return '5'; // just testing

    }

}

顺便说一句,我像这样通过 volt 函数扩展器访问 f1 函数

$compiler->addFunction('customfunc', function($resolvedArgs, $exprArgs) {
                return 'Testhelper ::f1('.$resolvedArgs.')';
});

如果有人可以帮助我,将不胜感激。

谢谢大家

【问题讨论】:

    标签: php class phalcon


    【解决方案1】:

    您正尝试在 Volt 中静态调用 TestHelper f1(),您的类不会将该函数公开为静态函数。

    您可以像这样更改您的代码:

    <?php
    
    use Phalcon\Mvc\User\Component;
    
    class Testhelper extends Component 
    {
    
        public static function f1($data) 
        {
            $tmp = self::f2($data);
            return $tmp;
        }
    
        public static function f2($data) 
        {
            return '5'; // just testing
        }
    }
    

    您的 Volt 功能将起作用。但是您必须记住,由于您是静态调用事物,您将无法立即访问 Component 提供的所有 di 容器服务,如下所示:

    $this->session
    $this->db
    

    您需要修改代码以使用 getDefault() 选择 di 容器

    另一种选择是使用您现在拥有的代码,但在您的 di 容器中注册 TestHelper,如下所示:

    $di->set(
        'test_helper',
        function () {
            return new TestHelper();
        }
    );
    

    然后您的 volt 函数将需要更改为:

    $compiler->addFunction(
        'customfunc', 
        function ($resolvedArgs, $exprArgs) {
            return '$this->test_helper->f1('.$resolvedArgs.')';
        }
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-06
      • 2018-02-16
      • 1970-01-01
      • 2018-07-30
      相关资源
      最近更新 更多