无法将变量传递给另一个类函数


Unable to pass varrible to another class function

这是在客户门户中使用Oracle RightNow平台,因此我无法完全访问所有代码。

在钩子上.php,它以前称之为:

$rnHooks['post_incident_create'][] = array(
    'class' => 'incident_create_model',
    'function' => 'send_email',
    'filepath' => ''
);

在incident_create_model.php中调用函数 send_email()

function send_email($data)
{
     //uses the variable $data to send an e-mail 
}

现在我想拆分该函数,所以我在incident_create_model.php中创建另一个函数,因此首先我修改钩子.php以调用新函数。

$rnHooks['post_incident_create'][] = array(
    'class' => 'incident_create_model',
    'function' => 'example',
    'filepath' => ''
);

我定义了新定义的函数 example() 并从中调用 send_email()

function example($data)
{
    send_email($data);
}

此操作失败并导致错误。有什么原因导致我无法传递变量$data吗?当我尝试在函数 example() 中访问变量时,我可以很好地访问变量。我认为这与我无权调用 example($data) 的隐藏代码有关,但我想不出任何可以阻止变量传递的东西。

在面向对象的范式中编程时,必须牢记方法范围。 在不$this的情况下调用send_email()意味着 send_email 方法在全局范围内。 但是,该方法实际上是在incident_create_model对象中定义的。 因此,通过将调用更改为 $this->send_email($data); ,PHP 知道您尝试调用的 send_email() 方法是在该类中定义的,而不是在某处作为过程方法定义的。

你现在钩子将始终实例化incident_create_model。 请记住,尽管静态调用send_email(),但self::send_email()意味着模型未实例化或不需要访问非静态的类方法和属性,因此在对send_email()方法执行的操作进行编程时,需要考虑到这一点。