Include a file in a class in PHP
Actually, you should append data to the variable.
<?php
/*
file.php
$hello = array(
'world'
)
*/
class SomeClass {
var bla = array();
function getData() {
include('file.php');
$this->bla = $hello;
}
function bye() {
echo $this->bla[0]; // Will print 'world'
}
}
?>
The best way is to load them, not to include them via an external file.
For example:
// config.php
$variableSet = array();
$variableSet['setting'] = 'value';
$variableSet['setting2'] = 'value2';
// Load config.php ...
include('config.php');
$myClass = new PHPClass($variableSet);
// In a class you can make a constructor
function __construct($variables){ // <- As this is autoloading, see http://php.net/__construct
$this->vars = $variables;
}
// And you can access them in the class via $this->vars array