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
/showthread.php 28 require_once





× This forum is read only. As of July 23, 2019, the UserSpice forums have been closed. To receive support, please join our Discord by clicking here. Thank you!

  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Generating navigation markup within function fails
#1
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?
  Reply


Messages In This Thread
Generating navigation markup within function fails - by mlam19 - 11-14-2018, 02:17 AM

Forum Jump:


Users browsing this thread: 2 Guest(s)