0

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'); 

    }

}

3 Answers 3

4

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;
       }
   }
Sign up to request clarification or add additional context in comments.

Comments

0

Try this

class frontproduct{

function fetchrange(){
ob_start();
   include('..config.php');

$val = ob_get_clean();
 return $val;
    }
}

Comments

0
// 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.