-2

This is my code in my class

    <?php
class PerClass
{
    private $sql_connection = null;
    private $localAf = '9929292F';
    function __construct($env) {
        // Nasty globals, sorry
        global $_config;
    $host = "localhost";
    $user = "user";
    $pass = "pass";
    $db = "kModule";


        // Build sql connection
        $this->sql_connection = new mysqli($host, $user, $pass, $db);
        // Check connection
        if ($this->sql_connection->connect_error) {
            die("Connection failed: " . $this->sql_connection->connect_error);
        }
    }

    public function getOrders($sSettingsId) {
        $query = <<<SQL
            SELECT * FROM `scrub_order_log` WHERE `scrub_settings_id` = {$sSettingsId} AND `order_date` BETWEEN (NOW() - INTERVAL (SELECT `c_h_days` FROM `scrub_settings` WHERE `id` = {$sSettingsId}) DAY) AND NOW() ORDER BY `order_date` DESC;
SQL;
        $result = $this->sql_connection->query($query);
        $resp = null;
        while ($row = $result->fetch_assoc()) {
            $resp[] = $row;
        }
        return $resp;
    }
}
?>

I am trying to get the output as shown in code below

<?
$details = $PerClass->getOrders('1');
print_r($details);
?>

But unfortunately I am getting following erro

Fatal error: Call to a member function getOrders() on null in /home/domn/public_html/stage/stage_test.php on line 37

Tried different ways but I think I am doing something wrong

user3550203
  • 699
  • 1
  • 7
  • 15
  • at what point do you instantiate the `$PerClass` object? – kidA Sep 01 '18 at 21:57
  • @kidA sorry for some reason the code was not displaying correct, so I edited the question again. Please see if it makes sense now. – user3550203 Sep 01 '18 at 22:00
  • 1
    The line that instantiates the PerClass is still missing.. Unless you forgot to add it to your code you need something like `$PerClass = new PerClass();` before the `$details = $PerClass->getOrders('1');` line. – kidA Sep 01 '18 at 22:06
  • yes when I initiate the PerClass. I get the result in array but it first line I get error – user3550203 Sep 01 '18 at 22:10
  • Warning: Missing argument 1 for PerClass::__construct(), called in /home/domn/public_html/stage/stage_test.php on line 37 and defined in /home/domn/public_html/stage/stage_test.php on line 6 @kidA – user3550203 Sep 01 '18 at 22:12
  • read the error message instead of copy-pasting it ! it tells you what the error is and how to fix it. In your constructor, you dont need/use the ***nasty global***, nor the parameter `$env`; – YvesLeBorg Sep 01 '18 at 22:19
  • do NOT use PHP short tags (``). You are asking for troubles as this can be turned off in php.ini – Marcin Orlowski Sep 01 '18 at 22:33

1 Answers1

0

The code that calls the getOrders method is missing the object instantiation.

<?
    // add this here
    $PerClass = new PerClass();
    $details = $PerClass->getOrders('1');
    print_r($details);
?>

now, because the constructor method of your PerClass expects you to pass in a value as an argument, this is going to result in the following warning:

WARNING Missing argument 1 for PerClass::__construct()

In order to resolve this warning you have two options:

  1. Pass the value of the $env parameter when you instantiate the object i.e. $PerClass = new PerClass('value_to_be_passed'); or
  2. Get rid of the $env argument in your constructor since - from what I can see - it is not used anywhere i.e. from function __construct($env) { ... } to function __construct() { ... }.

See this link for an interested discussion about using global in PHP.

kidA
  • 1,379
  • 1
  • 9
  • 19