-1

I'm sure this is an easy question, but I'm new to PHP and can't figure it out. I'm trying to parse a .ini file, which works correctly, but I somehow can't access the values.

My site is structured like this:

project/
    index.php
    inc/
        init.php
        classes/
            frontend.php
    msg/
        messages.ini

The messages file is parsed in init.php with $messages = parse_ini_file("msg/messages.ini"); and then included in index.php like this:

<?php
require_once('inc/init.php');

//printing the array here works!

$html = new frontend();

If I print the messages array from index.php everything works fine.

Yet when the index.php builds the new frontend(); the $messages array is not available there. The frontend itself is loaded in init.php and works fine without the ini file.

Thus I assume there is an import or variable scope issue, but I can't figure it out. Can someone point me in the right direction?

gmolau
  • 2,815
  • 1
  • 22
  • 45

2 Answers2

1

Instead of:

$html = new frontend();

... do something like:

$html = new frontend($messages);

PHP variable scope is rather simple anyway: a variable is either global or local to a function/method.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • I thought about passing the array as an argument, but shouldn't it have at least the same scope as the frontend if it is included in index.php? – gmolau Feb 09 '17 at 16:11
  • Scope is not so complex. There isn't a per-block or per-file scope. – Álvaro González Feb 09 '17 at 16:12
  • Thank you, I got it to work now. This also helped: http://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and – gmolau Feb 09 '17 at 16:28
-1

Try declaring $messages as global inside frontend().

Condorcho
  • 503
  • 4
  • 12