CodeIgniter Forums
global problem in helper - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: global problem in helper (/thread-4748.html)



global problem in helper - El Forum - 12-13-2007

[eluser]Gurkan OLUC[/eluser]
Hi,
I'm developing a web application and it requires to write my own helpers. So I create a new file named emlak_helper.php in system/helpers/ directory and write codes inside it. I have used global keyword to access variables that defined out of the function. But when I try to access variable after writing global $variable_name it returns nothing. Why it could be? Anyone have a idea?

helper file :

Code:
<?php

// Murat Arif ÇELİKER, [email protected]

// emlak helper dosyası
//

$durum_listesi = array('kiralik' => 'Kiralık', 'satilik' => 'Satılık');
$tip_listesi = array('daire' => 'Daire', 'villa' => 'Villa', 'dukkan' => 'Dükkan', 'buro' => 'Büro', 'mustakil' => 'Müstakil Ev',
                                                    'fabrika' => 'Fabrika', 'arsa' => 'Arsa', 'tarla' => 'Tarla', 'depo' => 'Depo', 'yazlik' => 'Yazlık',
                                                    'devremulk' => 'Devremülk', 'otel' => 'Otel', 'esyali' => 'Eşyalı Daire', 'devren' => 'Devren');
$doviz_listesi = array('ytl' => 'YTL', 'dolar' => '($) Amerikan Doları', 'euro' => '(E) Euro');
// durum listesini döndürür
//
function durum_liste($isim = 'durum', $secili = '')
{
    global $durum_listesi;
    
    print_r( $durum_listesi );
    
    // return (form_dropdown($isim, $durum_listesi, $secili));
}



global problem in helper - El Forum - 12-13-2007

[eluser]tonanbarbarian[/eluser]
Unfortunately just declaring a variable in a PHP file, rather than inside a function, does not always make it global scope.
It depends on where the file is included from.

If you have your index.php file and it includes function.php then any variables set in function.php will be in the global scope.

However if index.php calls a function my_includes(), and that function includes the function.php file then the variables set in function.php will be in the my_includes variable scope, not the global scope.

That is what is happening here. Because a function, or class method, is being used to include the helper files the variables declared do not have global scope.

simply declare the variables as $GLOBALS['doviz_listesi'] and they will be declared as global, or make the fiest line of your file another global declaration like you have inside the function.

Code:
index.php
<?php
// do something
...
include('function.php');
?>

function.php
<?php
$foo = 'bar';
function something() {
  global $foo; // this will work
}
?>

Code:
index.php
<?php
// do something
...
my_includes();
...
function my_includes() {
  include('function.php');
}
...
?>

function.php
<?php
$foo = 'bar';
function something() {
  global $foo; // this will not work
}
?>

<?php
// do something
...
my_includes();
...
function my_includes() {
include('function.php');
}
...
?>

function.php
<?php
$GLOBALS['foo'] = 'bar';
function something() {
global $foo; // this will not work
}
?>[/code]