-3

I'm new ;)

I have two file php (and one problem ;) ):

index.php:

...    
$lang = array();
$lang['En'] = 'Enable';
$lang['Dis'] = 'Disable';
...

and the class.print.php

    ...
include_once 'index.php';
class print{
?? echo $lang['En'] ??
}
    ...

what is the best way to echo a value of the array $lang directly from inside the class?

thanks :)

7
  • Pas it as a parameter to the constructor or the method that will use it. Kinda PHP 102. Commented Nov 22, 2013 at 14:16
  • or use the global keyword Commented Nov 22, 2013 at 14:16
  • print is a keyword, you won't be able to create a class named print. Commented Nov 22, 2013 at 14:17
  • 3
    Don't listen to @MightyPork, NEVER use globals. Commented Nov 22, 2013 at 14:17
  • 2
    @MightyPork it is not just ugly, it destroys the concept of OO which by the way PHP isn't actually too bad at. Every language has its advantages / disadvantages. I don't think the object model is funny imo. Commented Nov 22, 2013 at 14:22

1 Answer 1

2
include_once 'index.php';

class my_class {
  function __construct(&$lang) {
    var_dump($lang);
  }
}

$my_object = new my_class($lang);
Sign up to request clarification or add additional context in comments.

8 Comments

You should be using __construct() really..
no, its the same.. using __construct or function with class name...
yes, thats advantage :)
Fully read the answer to the question, you really shouldn't be using the class name for the constructor.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.