[英]Not able to get return output from PHP class
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致命错误:在第 37 行的 /home/domn/public_html/stage/stage_test.php 中的 null 上调用成员函数 getOrders()
Tried different ways but I think I am doing something wrong尝试了不同的方法,但我认为我做错了什么
The code that calls the getOrders
method is missing the object instantiation.调用
getOrders
方法的代码缺少对象实例化。
<?
// 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:现在,因为
PerClass
的构造函数方法希望您将值作为参数传递,这将导致以下警告:
WARNING Missing argument 1 for PerClass::__construct()
警告 PerClass::__construct() 缺少参数 1
In order to resolve this warning you have two options:为了解决此警告,您有两个选择:
$env
parameter when you instantiate the object ie $PerClass = new PerClass('value_to_be_passed');
$env
参数的值,即$PerClass = new PerClass('value_to_be_passed');
or$env
argument in your constructor since - from what I can see - it is not used anywhere ie from function __construct($env) { ... }
to function __construct() { ... }
.$env
参数,因为 - 从我所看到的 - 它没有在任何地方使用,即从function __construct($env) { ... }
到function __construct() { ... }
。 See this link for an interested discussion about using global
in PHP.有关在 PHP 中使用
global
的有趣讨论,请参阅此链接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.