Dizi sınıfta nulled becamed

3 Cevap

hay my class

class language{

    function merge($filename = ''){	
// the path is true
    return include_once ('language/'.$filename);
    }

}

Tamam bu lang testi

en.php

<?php

if (empty($lang) || !is_array($lang))
{
    $lang = array();
}

$lang = array_merge($lang, array(
    'test'		=> 'Home Page'

));

?>

i eklerseniz

echo $lang['test'];

en.php onun dönüş Ana Sayfa

ama ben sınıf yapmak

$l = new language;
$l->merge('en.php');

its not working i checked it

var_dump($lang);

o null döndürebilir

3 Cevap

Eğer bir şey dönmek istiyorsanız include_once Eğer dosyada bir şey dönmek zorunda diyoruz. Bkz Example 5.

<?php // return.php
    $var = 'PHP';
    return $var;
?>

vs

<?php // noreturn.php
     $var = 'PHP';
?>

verir

<?php
    $foo = include 'return.php';
    echo $foo; // prints 'PHP'

    $bar = include 'noreturn.php';
    echo $bar; // prints 1
 ?>

Eğer birleştirme işlevi kapsamda en.php dosyasını dahil ediyoruz, çünkü $lang değişkenleri yok. Dan PHP manual ...

When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope.

Eğer bu fonksiyonu global beyan olmadıkça zaman merge döner, dolayısıyla $lang kaybolur.

Benim soultion görmek

function __construct(){ 
$this->lang =  array();
}

class language{

    function merge($filename = ''){     
include_once ('language/'.$filename);

    return $this->lang = $lang;
    }

}

and when use echo $l->lang['test'];