“Notice: Undefined property: [extension_name]::$_state” Error in Joomla

When working on a major project on a Joomla website, we usually maximize the Joomla error reporting. This guarantees that not a single error, not even a notice, can go undetected and unfixed in our final work.

One of the common notices that we see on major projects is the following:

Notice: Undefined property: [extension_name]::$_state in /libraries/joomla/session/session.php on line 323

So, probably the first question that you have is the following: “What happens in line 323?”

Well, here’s the code in line 323 in the session.php file located under the /libraries/joomla/session/.

if ($this->_state === 'destroyed')

If you’re a PHP programmer, you probably know what the problem is. The attribute _state on the JSession object is not set, and that’s why PHP is complaining. It is not really an error, but PHP thinks that the value of _state might be needed at one point or the other in your code.

But why the _state attribute was not set?

Good question! Take a look at this code:

$sessionId = JSession::getId();

If you have used the JSession class before, you will know that the getId() method (quick note: a method is a function that belongs to a class) is not really static, which means that even though the above code works, it’s wrong…

You see, you must instantiate an object of type JSession, and then call the method getId(). So, you must replace the above code with this one:

$objSession = new JSession();
$sessionId = $objSession->getId();

The above code ensures that all attributes, including the _state attribute that PHP is complaining about, are properly set in the constructor of the JSession class.

But, what if you don’t want to instantiate an object?

The above solution that we have proposed is the right one. But, in some cases, you may not want to instantiate an object (we can’t think of any at the moment, but we’ve done this to address weird bugs). In this case, there are two methods to solve the problem:

  1. At the extension’s level

    All you need to do is to open the main .php file of the extension that PHP is complaining about, and change your code from:

    $sessionId = JSession::getId();

    to

    $sessionId = @JSession::getId();

    (Notice the @ symbol just in front of JSession.)

  2. At the core level

    Just change the line 323 in the session.php file which is located under the /libraries/joomla/session/ directory from:

    if ($this->_state === 'destroyed')

    to

    if (@$this->_state === 'destroyed')

    (Again, notice the @ sign at the beginning of the if statement).

If you have many of those errors on your website, then the second method might be more practical, but you must consider the fact that you are changing a core Joomla file, that might be overriden with a new update.

Isn’t there an easier way to fix the problem for those who are not programmers?

Yes there is. All you need to do is to login to your Joomla’s backend, and then go to Site -> Global Configuration -> Server, and then choose None next to Error Reporting and finally click on Save at the top right. This is quite easy, isn’t it? But, on the flip side, you haven’t really fixed the error, you have just hidden it, and if you are in a development environment, then you won’t see that subtle error that may or may not be causing a cascade of issues on your website. (Note that in a production environment, the value of Error Reporting should always be None. Otherwise, your Joomla website can reveal some valuable information that can be used to launch malicious attacks against it!)

So, what is the best method to fix this problem?

The best method to fix this problem is to ensure that the method getId() is never invoked statically. Generally, static calls to non-static methods may result in stability issues and may haunt you back when you least expect it!

Now, we’re talking to you directly our valuable reader and potential client! If you are seeing errors (such as the above error or any other error) on your Joomla website and you need them fixed the right way and right away (is that a rhyme?), then look no further. Just contact us at itoctopus and you can rest assured that we’ll fix these errors in no time and with no side effects on your website! By the way, our fees are very affordable, and we’re very friendly, so you really have no excuse for not calling!

No comments yet.

Leave a comment