The following warnings occurred:
Warning [2] Undefined variable $unreadreports - Line: 26 - File: global.php(961) : eval()'d code PHP 8.2.25 (Linux)
File Line Function
/global.php(961) : eval()'d code 26 errorHandler->error
/global.php 961 eval
/printthread.php 16 require_once



UserSpice
Generating navigation markup within function fails - Printable Version

+- UserSpice (https://userspice.com/forums)
+-- Forum: Support Center (https://userspice.com/forums/forumdisplay.php?fid=23)
+--- Forum: UserSpice 4.3 and Below (https://userspice.com/forums/forumdisplay.php?fid=26)
+--- Thread: Generating navigation markup within function fails (/showthread.php?tid=1211)



Generating navigation markup within function fails - mlam19 - 11-14-2018

Greetings,

I would like to write a class that contains function that serves as a sort of wrapper around some UserSpice 4 features. I'm mostly concerned with controlling when and where in my own program the UserSpice HTML markup is generated and used. Unfortunately, I've run into some problems using UserSpice code from within functions.

To start, first I modified step 5 from the Getting Started instructions. I skipped the generation of the header, and saved the output of the navigation markup to a variable. The following script works when no user is logged in, and when a user is logged in (script #1):

PHP Code:
<!DOCTYPE html>
<
html>
    <
head>
        <
title>Works logged in and out</title>
    </
head>
    <
body>
<?
php

require_once "users/init.php";

// Stuff not initialized in UserSpice init script, but should be.
$db DB::getInstance();
$settingsQ $db->query("Select * FROM settings");
$settings $settingsQ->first();

// Verify that user object exists at this point.
dump($user);

ob_start();

require_once 
"users/includes/navigation.php";

$navigationMarkup ob_get_contents();

ob_end_clean();
?>
        <?=$navigationMarkup;?>
    </body>
</html> 

Next, I moved the code into a function as follows (script #2):

PHP Code:
<!DOCTYPE html>
<
html>
    <
head>
        <
title>Only works when logged out</title>
    </
head>
    <
body>
<?
php

function initialize() {

    require_once 
"users/init.php";

    
// Stuff not initialized in UserSpice init script, but should be.
    
$db DB::getInstance();
    
$settingsQ $db->query("Select * FROM settings");
    
$settings $settingsQ->first();

    
// Verify that user object exists at this point.
    
dump($user);
    
    
ob_start();

    require_once 
"users/includes/navigation.php";

    
$navigationMarkup ob_get_contents();

    
ob_end_clean();

    return 
$navigationMarkup;
}
?>
        <?=initialize();?>
    </body>
</html> 

Script #2 works when nobody is logged in, but after logging in an error occurs when processing navigation.php. The following appears in the Apache error log (I have replace the paths with --- but the remainder should be sufficient):

Code:
[client 127.0.0.1:52386] PHP Fatal error:  Uncaught Error: Call to a member function data() on null in ---/users/helpers/us_helpers.php:1010
Stack trace:
#0 ---/users/includes/database-navigation.php(56): hasPerm(Array, '1')
#1 ---/users/includes/navigation.php(119): require_once('---..')
#2 ---/test2.php(23): require_once('---...')
#3 ---/test2.php(32): initialize()
#4 {main}
thrown in ---/users/helpers/us_helpers.php on line 1010

The data() function is apparently a member of the global $user variable. $user seems to exists before processing navigation.php, and I am uncertain when and why it becomes null.

Is there some other way to massage things to get the function working?


RE: Generating navigation markup within function fails - mudmin - 11-14-2018

What happens if you put

global $user;

on the line right after
function initialize() {


RE: Generating navigation markup within function fails - mlam19 - 11-15-2018

(11-14-2018, 03:33 PM)mudmin Wrote: What happens if you put

global $user;

on the line right after
function initialize() {

That makes it work. Thanks!