0

I'm new to PHP and i want to understand the following case and how to find a working solution. I have two script config.php and index.php

Config.php

<?php

$ip = 'localhost';
$username ='patrick';
$password = 'password';

?>

Index.php

<?php
require 'config.php';
class index extends PHPUnit_Framework_TestCase{
private $index;

    protected function setUp()
    {
        $this->index= NULL;
    }

    protected function tearDown()
    {
        $this->index= NULL;
    }

    public function testGetConfig()
    {
        echo $ip // How do I pass the value to this function?
    }

}
?>

How do I pass the value from a script to the function in a class.

  • You can store your config in an individual class which return it from static function. – Askme23 Jun 10 '20 at 13:07
  • You don't get to pass values to PHPUnit tests (it's all controlled by the framework). You have to load the values in the test method, by moving the require statement into testGetConfig, for instance. – Peter Jun 10 '20 at 13:08
  • @Peter, okay that's something new to me.I'm trying to access the value but keep on getting error passing null variable – Jeeva Suriyaa Jun 10 '20 at 13:09
  • 1
    Not sure what you mean by getConfig. You have written a method named testGetConfig. If you move the require statement from the global scope into the method, the variables declared in config.php are available in the method. – Peter Jun 10 '20 at 13:12
  • @Peter, ah i get it. So let's say if i want to use the same config in multiple function so i just require 'file.php' to each function? – Jeeva Suriyaa Jun 10 '20 at 13:17
  • If you don't control the arguments to the function, yes. – Peter Jun 10 '20 at 13:19
  • 1
    I'd think requiring the file inside `setUp` and setting the values as object properties makes the most sense, then they're available in all further methods… – deceze Jun 10 '20 at 13:20
  • @deceze that makes sense. So my setup function will look like this ? protected function setUp() { require 'config.php'; $this->upsell = new upSellCore(); $this->odb_user = $odb_user; $this->odb_pass = $odb_pass; $this->odb_host = $odb_host; $this->odb_name = $odb_name; } – Jeeva Suriyaa Jun 10 '20 at 13:36
  • @Peter, so i have to build many private variable to store those value for later on? – Jeeva Suriyaa Jun 10 '20 at 13:36

0 Answers0