注意:试图获取..中非对象的属性时出错


Notice: Trying to get property of non-object in... error

我在'href' => 'index.php?action=profile&user='.escape($user->data()->username).'', 这条线上遇到问题

当用户注销时,我会收到错误,但当我登录时,我不会收到错误。

function toolbar_section()
{
    $user = new User();
    global $variables;
    $variables['menu_buttons'] = array(
        'home' => array(
            'title' => 'Pocetna',
            'href' => 'index.php',
            'show' => true,
        ),
        'profile' => array(
            'title' => 'Profil',
            'href' => 'index.php?action=profile&user='.escape($user->data()->username).'',
            'logged' => true,
        ),
        'logout' => array(
            'title' => 'Odjava',
            'href' => 'index.php?action=logout',
            'logged' => true,
        ),
    );
}

你必须像一样在之前检查条件

代码:

function toolbar_section()
{
    $user = new User();
    global $variables;
    if(count($user) > 0)
    {
       $href =  'index.php?action=profile&user='.escape($user->data()->username).'';
    }
    else
    {
       $href = 'index.php?action=profile&user='.escape("abc").'';
    }
    $variables['menu_buttons'] = array(
        'home' => array(
            'title' => 'Pocetna',
            'href' => 'index.php',
            'show' => true,
        ),
        'profile' => array(
            'title' => 'Profil',
            'href' => $href,
            'logged' => true,
        ),
        'logout' => array(
            'title' => 'Odjava',
            'href' => 'index.php?action=logout',
            'logged' => true,
        ),
    );
}