将对象传递给对象时如何访问私有属性

时间:2019-03-07 22:59:17

标签: php

我在prop上有“设置”类:

  • 私人$ dbo
  • 私人$ user

接下来,在设置Im创建obj的方法中:$ file = new Docfile(); 我打电话给这样的东西:

echo $card->getCardHTML('card','categories', $file->includeFile('view/settingsCategory.php', $this));

我在Docfile类中的方法

function includeFile($src, $callingObj)
{
    ob_start();
    if (file_exists($src)) {
        include $src;
    } elseif (file_exists('../'.$src)) {
        include '../'.$src;
    }
    $stringWithFile = ob_get_clean();
    return $stringWithFile;
}

在我使用的文件中,例如:

$callingObj->user->getUserLogin()

然后我得到了错误:

Fatal error:Uncaught Error: Cannot access private property Settings::$user in C:\xampp\htdocs...

我可以使用公共财产,但我认为这不是一个好的解决方案。 我该如何实现?我还需要传递完整的对象,因为我不仅使用属性,还使用方法。

也许还有其他更好的方法可以将文件包含在HTML代码中?我需要将此文件内容作为变量,因为我正在像这样使用它:

$output .= '<div>'.$stringWithFile.'</div>';

0 个答案:

没有答案
相关问题