Joomla module and variables within functions - php

I have created a module in Joomla and all is working fine, but when I put a function in and try and access a variable it does not work, but if I echo it outside the function it is ok
$item_img = $params->get('item_img','modules/mod_k2_mobile/images/item_icon.gif');
// not working
function GetIMG(){
global $item_img;
echo "item".$item_img;
}
GetIMG();
// working
echo "item".$item_img;
why?

I would advice you not to use any global variables as long as you can avoid it. Can't you change your function declaration to something like "GetIMG( $item_img )"?
If you still want to use a global variable, this should work:
**global $item_img;**
$item_img = $params->get('item_img','modules/mod_k2_mobile/images/item_icon.gif');
// not working
function GetIMG(){
global $item_img;
echo "item".$item_img;
}
GetIMG();
// working
echo "item".$item_img;
I hope it helps!

Related

What should I depend on if php functions are not called immediately?

I've tried regular functions and call_user_function, both which do not finish executing before they get to the next line after a function call. What do people use instead? Should I just include a separate php file for every "function" I want to run?
edit:
$IDNum = 0;
$restartButtonIDName;
$playButtonIDName;
$audioPlayerIDName;
$MP3AudioSourceIDName;
$OGGAudioSourceIDName;
$resultingTextIDName;
$currentTimeIDName;
$checkOffsetIDName;
call_user_func('setIDNames');
function setIDNames() {
echo "called setIDNames <br />";
call_user_func('incIDNums');
$restartButtonIDName = "restartButton".$IDNum;
$playButtonIDName = "playButton".$IDNum;
$audioPlayerIDName = "audioPlayer".$IDNum;
$MP3AudioSourceIDName = "MP3AudioSource".$IDNum;
$OGGAudioSourceIDName = "OGGAudioSource".$IDNum;
$resultingTextIDName = "resultingText".$IDNum;
$currentTimeIDName = "currentTime".$IDNum;
$checkOffsetIDName = "checkOffset".$IDNum;
}
function incIDNums(){
echo "called setIDNums <br />";
$IDNum += 1;
}
echo $restartButtonIDName." test<br />"; // echos "test" not the resulting name
?>
Variables accessed inside a function are not the same as variables outside the function, or in a different function, unless you declare the variables with a global statement inside the function.
$IDNum = 0;
incIDNums();
echo $IDNum; // Will echo 1
function incIDNums(){
global $IDNum;
echo "called setIDNums <br />";
$IDNum += 1;
}
It's generally considered poor style to depend heavily on global variables, as it impairs the generality of the functions. The function should get its input via parameters, and sends the results as a value using return. If you need to return multiple results, you can package them into an array, or use reference parameters that are updated in place.
Probably the problem you have is due to the scope of variables ,please note the lifetime of variables inside a function is only until function is active to make changes effect to global variables which are outside of function you need to make them global inside function like:
function setIDNames() {
global $restartButtonIDName ,$playButtonIDName ,$audioPlayerIDName ...;//All variables you wanna use saperated by comma.
//Your code
}

PHP how come global variable does not work when utlizing ajax to fetch content?

So I have this code snippet.
function get_box_ajax() {
global $box;
$box = get_box(); // creates box object
ob_start();
get_template( '/filepath/content.php' );
$output = ob_get_clean();
}
// in the content.php file
global $box;
<form action="<?php echo box_url( $box->url ); ?>" method="post"> // error on this line
...
</form>
So with this code, I am getting a non-object error on the call to $box->url. Take note this is done via ajax.
So I thought in my ajax function I have already globalized $box and that will take but it doesn't seem to work? Any thoughts?
Two things:
When is your get_box_ajax function called? And what does the function get_box do? Both things are relevant.
I don't think the problem is whether box is global or not (which it is), but rather if the url variable of box is being defined or if box is being initialized at all.
Set box to null before the function. There's no reference in the global scope
$box = null;
function get_box_ajax() {
global $box;
$box = get_box();
Change the start to that

Is there a way to set Global Variables to be available by default within PHP functions?

Is there any way to do this / specify this in the php.ini configuration file? It would be really nice, at least for local server purposes, just to write functions without having to write the global keyword every time a global variable is used within the method.
Any way to do this?
EDIT:
What I mean is being to simply write this:
Example file "index.php":
$MY_ARRAY = array();
include("functions.php");
And then in "functions.php":
function addToArray($pMessage) {
$MY_ARRAY[] = "<a href='somelink.php'>$pMessage</a>";
}
Instead of:
And then in "functions.php":
function addToArray($pMessage) {
global $MY_ARRAY;
$MY_ARRAY[] = "<a href='somelink.php'>$pMessage</a>";
}
Yeah! You can setup a prepend file in PHP.
See: http://www.php.net/manual/en/ini.core.php#ini.auto-prepend-file
I still do not understand the purpose. But if I need to just remove the GLOBAL keyword from mentioning everytime, I would prefer to use a class with static array.
Something like this:
<?php
// Config.php
class Config {
public static $MY_ARRAY = array();
}
?>
And then include this file from your loader just call like this:
function addToArray($pMessage) {
Config::$MY_ARRAY[] = "<a href='somelink.php'>$pMessage</a>";
}
That will work.

PHP includes/require in a file and all functions inside it

I have a file which I include say:
require_once("../../constructor/database.php");
$stuff_inside_include_file->doThis() ; //ACCESSIBLE
function createUser(){
$stuff_inside_include_file->doThat(); //NOT-ACCESSIBLE (object not found)
}
function deleteUser(){
. ...
If I move the require statement into the scope of the function, then it works. Otherwise, it doesn't. Is there a better solution?
Thank you in advance.
You can try:
function createUser($stuff_inside_include_file){
$stuff_inside_include_file->doThat();
}
it should work, I think.
The best solution would be to not create global variables, but this would also work:
require_once("../../constructor/database.php");
$stuff_inside_include_file->doThis() ; //ACCESSIBLE
function createUser(){
global $stuff_inside_include_file;
$stuff_inside_include_file->doThat(); //ACCESSIBLE
}
function deleteUser(){
. ...
But really.. "Think globally, act in local scope".
I believe this is an issue with the variable not being global.
e.g.
$i = 10;
function pooppoop() {
$i = 11;
#this only sets the local variable i to 11
}
So you will want to put a global $i; at the top of your function. Obviously replace $i with the variable you are having trouble accessing.
I looks like the $stuff_inside_include_file variable is in the global scope, so you should be able to do this:
function createUser(){
global $stuff_inside_include_file;
$stuff_inside_include_file->doThat();
}

php newbie question: global variables for web

i'd like to set up some global variables for storing several things.
i've tried it like this:
function init_web()
{
$webname = "myweb";
$web['webname'] = $webname;
$web['server_root'] = $_SERVER['DOCUMENT_ROOT']."/$webname/";
$web['lang']="en";
}
the problem is that i can't access those variables inside of functions ..
i've tried using global $web; but didnt help.
what's the trick to get it global?
thanks
While you'll get the usual "global variables are bad" crying, here's the basics:
$web = array(); // define the var at the "top level" of the code tree, outside any functions/classes.
function init_web() {
global $web; // make it visible in the function
$web['lang'] = 'en'; // make some settings
}
basically, you had it, but hadn't defined the variable outside the function. Just saying 'global' within the function won't magically create one outside the function - it already has to exist before you try to "internalize" it to the function and change/access its contents.
You are on the right track:
$web = array();
function init_web()
{
global $web;
$webname = "myweb";
$web['webname'] = $webname;
$web['server_root'] = $_SERVER['DOCUMENT_ROOT']."/$webname/";
$web['lang']="en";
}
You can define them constant
define('WEBNAME',"myweb");
and can use everywhere in your application because constant are global in nature by default.
and this is the way to store constant as constant as they never changed dynamically until you move to new server or change configuration.
you can use session variables:
session_start(); // at the top of the php page
function init_web()
{
$webname = "myweb";
$_SESSION['webname'] = $webname;
$_SESSION['server_root'] = $_SERVER['DOCUMENT_ROOT']."/$webname/";
$_SESSION['lang']="en";
}
now they can be 'globally' accessable :-)
Declare $web outside the function and reference it inside with the $GLOBALS superglobal:
// Declare in global scope
$web = array();
function init_web()
{
$webname = "myweb";
// Access via superglobal in function scope
$GLOBALS['web']['webname'] = $webname;
$GLOBALS['web']['server_root'] = $_SERVER['DOCUMENT_ROOT']."/$webname/";
$GLOBALS['web']['lang']="en";
}
If you're just storing scalar values (strings, ints, floats - not arrays, objects), you should use define().
This will make your configurations global and constant.
As to answer your question,
First define your variables outside the scope of that function (perhaps in a config file) and then use the global keyword to make it global when you need them.

Categories