!!! Undefined variable user

GK User
Sat Oct 18, 2014 7:26 pm
All pages(latest) generate php notes at the end of page:
Blog/Latest items(1x,2x,3x,):
Code: Select all
Notice: Undefined variable: user in /var/www/projects/maxix/templates/gk_writer/html/com_k2/templates/default/latest.php on line 92
Notice: Trying to get property of non-object in /var/www/projects/maxix/templates/gk_writer/html/com_k2/templates/default/latest.php on line 92


Could you please fix it?
User avatar
Fresh Boarder

teitbite
Sun Oct 19, 2014 1:07 pm
Hi

Please do not worry about it. It's just a notice shown because of different standards in php programming nowadays. Anyway everything will work correctly. Please simply disable showing errors in global settings for server in joomla panel.
User avatar
Moderator

GK User
Tue Oct 21, 2014 7:07 pm
This is the interesting way to fix the problem. You said that "Undefined variable" is a:

"It's just a notice shown because of different standards in php programming nowadays"

Could you please give me a direction, what version of php doesn't throw the notice if variable is undefined?
User avatar
Fresh Boarder

teitbite
Wed Oct 22, 2014 1:19 pm
Hi

A restriction to define variables was a must couple years ago. I know PHP is still showing the message, but just looking at the exampe given by You.

Code: Select all
                              <?php if($user->feed && $this->params->get('userFeed')): ?>


Above is the line which throws this message. Now lets look into /componens/com_k2/controllers/latest.php which is a file called before layout file. In line 23 we see:

Code: Select all
        $user = JFactory::getUser();


So it is defined, but just not in the same file. So why is this message for ? Just a relict of an ancient programming times :)
User avatar
Moderator

GK User
Wed Oct 22, 2014 6:41 pm
Hi,
Oh - "Just a relict of an ancient programming times". It's not ancient but wrong programming. To fix the warnings a programmer should check variable before use one. This is the right way to do it (it removes notices):

Code: Select all
<?php if(isset($user) && $user->feed && $this->params->get('userFeed')): ?>


Also, I found other bad programming style. But in Wordpress "Writer" theme. You use utf8 in your templates but use one byte strings function to manipulate strings. For example in gk_writer/wp-content/themes/Writer/template.overlay.php you must use:
Code: Select all
<?php echo mb_substr(get_the_excerpt(), 0, get_theme_mod('writer_overlay_text_limit', '200'),'UTF-8') .'...'; ?>


But your code use substr version. And it leads to wrong display output (questions in rumbus at the end of substring - incorrect utf8).
I used it in russian language and it was a real pain in @$ to fix your code. Finally I tried the Joomla version and unfortunately it is not ideal too.

Anyway thanks.
User avatar
Fresh Boarder

teitbite
Fri Oct 24, 2014 9:08 am
Hi

I'm very old so I get what You mean :) I still define all variables because I was taught this way. And even if I agree with You it cannot be called "bad programming". Modern programming languages does not require to definitions. PHP is going the same way, that's why "Undefined variable" was an error in PHP 4 and is just a notice in newer versions.

I'll let programmers know about multi bite string functions.
User avatar
Moderator


cron