Required Files and Variable Scope

I am self-teaching PHP and I tried searching for this but I don’t really know what terms to even search for so sorry if this is a repeat question.

I have a file, main.php. At the top of that page I have require_once(sessions.php) which is a class where I keep everything related to sessions. I then have a require_once(header.php) which pulls in the default header for each page. When I want to access sessions data I can easily do so from within the main.php page, but if I want to access sessions data from header.php the only way I have found is to use global. Is this the best practice or is there a better way to go about this? My code in the header.php file is below, it works like intended but I don’t feel right about the way I did it.

[php]<?php
global $session;
echo get_full_name($session->userid);
?>[/php]

You can go a few different ways. Session variables are global by default without your addition for one. 2, you can create a static class to get them. 3, get_full_name, sounds like it should be apart of a user class, not all by itself.

Sponsor our Newsletter | Privacy Policy | Terms of Service