Jump to content

$session object unavailable in PHP function in template


ErikMH
 Share

Recommended Posts

So, if I do this right at the top of a template PHP file:

<?php
	namespace ProcessWire;
	function myFunction() {
		global $config;
		echo $config->dateFormat;
	};
?>

I, predictably, get this in my front end (once I figured out about the necessary global $config; line):

Y-m-d H:i:s

Happy camper. But if I try to access $session variables in the same way:

<?php
	namespace ProcessWire;
	function myFunction() {
		global $session;
		echo $session->future;
	};
?>

I’m told there is no $session object; it responds exactly as if I hadn’t included the global $session statement:

PHP Notice: Trying to get property 'future' of non-object in .../public/site/templates/home.php:5

I thought the problem might conceivably have to do with my private variables, so I tried the provided hasCookie() method:

<?php
	namespace ProcessWire;
	function myFunction() {
		global $session;
		print_r($session->hasCookie());
	};
?>

With essentially the same result:

Error

Call to a member function hasCookie() on null

$session variables and methods are fully accessible and work exactly as expected if placed immediately outside of myFunction().

I am rapidly running out of hair. Is this intentional? Or a bug? Can anyone confirm that they see this behavior? Can anyone think of a workaround? Thanks!

ProcessWire: 3.0.179
PHP: 7.4.19
 

Link to comment
Share on other sites

Just read this quickly...

  1. ProcessWire variables such as $config, $session, $page, etc need to be accessed differently inside functions (check out PHP function scope). One way to access them is via wire, e.g. wire('config'), wire('session'), wire('page'), etc.
  2. ProcessWire variables are already global. I don't understand why you are using the PHP global keyword.

Others will chime in, with better answers ?.

  • Like 2
  • Thanks 1
Link to comment
Share on other sites

Thanks for your response, @kongondo! To answer your second point, I had indeed thought that ProcessWire variables were global — but since neither the $config nor the $session variable actually were accessible in my function (see the errors), I researched and found that:

Quote

A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function

— https://www.w3schools.com/PHP/php_variables_scope.asp

That source then went on to explain that that’s what the “global” statement is for. And in fact, adding “global $config” to the function solved the problem for $config. But when “global $session” did not solve the problem for $session, I posted here looking for guidance.

Thank you for the pointer to wire, which I’ve never heard of and which didn’t come up in the entire day that I spent searching the PW docs, blogs, tutorials, and forum. I’ll experiment with wire('session') and see whether that solves my problem.

I’m loving ProcessWire itself, but the layers upon layers of documentation and forum posts and blog articles that you have to wade through in hopes that someone has been kind enough to provide a link to the information that you really need is very frustrating.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...