Reputation: 606
i use common config file for admin and frontend template now i want to include it in function file how to include it once and use it in all file.
class frontproduct{
function fetchrange(){
include('..config.php');
}
}
Upvotes: 0
Views: 2078
Reputation: 24645
Here's a list in order of best to worst practice
1: include and inject into class via constructor
include("config.inc.php");
$fp = new frontproduct($config);
2: include and inject via setter (the "optional dependancy" method)
include("config.inc.php");
$fp = new frontproduct();
$fp->setConfig($config);
3: pass into function calls (the "aren't objects supposed to be easier" method)
include("config.inc.php");
$fp = new frontproduct();
$fp->doSomething($config, $arg);
$fp->doSomethingElse($config, $arg1, $arg2);
4: import in class (aka the "silent dependency method")
class frontproduct{
public function __construct(){
include('config.inc.php');
$this->config = $config;
}
}
5: static property assignment (aka the "at least its not a global" method)
include ("config.inc.php");
frontproduct::setConfig($config);
6: global assignment (aka the "what is scope" method)
include ("config.inc.php");
class frontproduct{
public function doSomething(){
global $config;
}
}
Upvotes: 4
Reputation: 5971
// myfile.php
include('../config.php');
class frontproduct {
function fetchrange(){
}
}
You should include configuration file before class code, and please make sure you are understand how you should use relative paths.
Upvotes: 0
Reputation: 154
Try this
class frontproduct{
function fetchrange(){
ob_start();
include('..config.php');
$val = ob_get_clean();
return $val;
}
}
Upvotes: 0