doupeng8494 2019-08-05 10:33
浏览 296

如何在PHP中调用外部函数

I want to call an external function in PHP from inside of a class function, but it does not work.

I've tried with include and require at various locations of the PHP class file, but without success.

The following function is located in the file Amd.php

<?php

function AmdGetFirstName($sUsername){
    $oUserModel = new UserCoreModel;
    $iProfileId = $oUserModel->getId(null, $sUsername);
    $sFirstName = $oUserModel->getFirstName($iProfileId);
    unset($oUserModel);
    return $sFirstName;
}

Now I want to call this function from a class function with the following code:

<?php

require_once PATH_ROOT . '/Amd.php';

class Design
{
    public function getProfileLink($sUsername)
    {
        $sFirstName = AmdGetFirstName($sUsername);

        $sHtml = '<a href="';
        $sHtml .= (new UserCore)->getProfileLink($sUsername);
        $sHtml .= '" title="' . t("%0%'s profile", $sFirstName) . '">'; 
        $sHtml .= $sFirstName . '</a>';

        echo $sHtml;
    }
}

When I debug the code the function AmdGetFirstName is not called from within the public function getProfileLink. If I call the same function in the index.php file with the same require_once statement it works very well. The difference what I can see is that in index.php the function is not called within another function or class function. I also tried to locate the require_once statement inside of the class function with the same result.

  • 写回答

2条回答 默认 最新

  • dongxu7408 2019-08-05 10:52
    关注

    I suggest to include the Amd.php with include on using dirname(FILE) like bellow:

    include (dirname(__FILE__)."/Amd.php")
    
    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办