Php 通过includes访问不同的类
我在访问类时遇到问题 index.phpPhp 通过includes访问不同的类,php,class,include,Php,Class,Include,我在访问类时遇到问题 index.php include('includes/header.php'); include('includes/step1.php'); include('includes/footer.php'); header.php session_start(); include('php/classes/Errorhandler.php'); include('php/classes/User.php'); $errorhandler = new Error
include('includes/header.php');
include('includes/step1.php');
include('includes/footer.php');
header.php
session_start();
include('php/classes/Errorhandler.php');
include('php/classes/User.php');
$errorhandler = new Errorhandler();
$user = new User();
// Test
print_r($errorhandler->errors);
...html
// load the class Errorhandler
require_once('php/classes/Errorhandler.php');
// load the class User
require_once('php/classes/User.php');
// create errorhandler object
$errorhandler = new Errorhandler();
// create user object
$user = new User();
print_r($errorhandler->errors);
Errorhandler.php
class Errorhandler {
public $errors = array();
...
}
class Errorhandler {
function error() //create a function like this
{
$error= /*any error*/;
return $error;
}
}
User.php
class User {
public function __construct() {
if($this->grab_computerid()) {
if(!$this->grab_mandant()) {
$errorhandler->errors[] = "102: There was an error.";
}
if(!$this->grab_os()) {
$errorhandler->errors[] = "103: There was an error.";
}
} else {
$errorhandler->errors[] = "101: There was an error.";
}
}
private function grab_computerid() {
$sqlconnection = new SqlConnection();
$conn = $sqlconnection->db_connect();
if ($conn) {
$query = "SELECT Computer_Idn FROM " . DB_PC_TABLE . " WHERE DeviceName = ?";
$params = array($this->get_hostname());
if ($sqlconnection->query($query, $params)) {
$computer_id = $sqlconnection->fetchRow();
$this->set_computer_id($computer_id['Computer_Idn']);
return true;
echo "Test";
} else {
$errorhandler->errors[] = "Statement error occurred.";
}
} else {
$errorhandler->errors[] = "Can't connect to database.";
// test
print_r($errorhandler->errors);
}
$sqlconnection->db_disconnect();
}
}
php包含构建站点的相关部分。在header.php中,我创建了两个对象(1.errorhandler,2.user)。用户类检查sqlconnection的返回值(布尔值)。我知道,我使用了错误的密码,得到了一个错误的密码。因此,if($conn)正确地打印print\r($errorhandler->errors)。但是当我想在step1.php中显示错误时,数组错误是空的
step1.php
// show negative messages
if ($errorhandler->errors) {
foreach ($errorhandler->errors as $error) {
echo '<div class="alert alert-danger message"><strong>Error: </strong>' . $error . '</div>';
}
}
我在类用户中设置了一个错误:
$errorhandler->errors[] = "Can't connect to database.";
header.php中的数组$errorhandler->errors为空。必须调用函数。您不能直接获得值 第一个->
$errorhandler=新的errorhandler()代码>
第二->Errorhandler.php
class Errorhandler {
public $errors = array();
...
}
class Errorhandler {
function error() //create a function like this
{
$error= /*any error*/;
return $error;
}
}
第三->
if($err=$errorhandler->error()){
foreach($err as$error){
回显“错误:”.$Error;
}
}
还可以尝试使用require_once()而不是include() 尝试使用require\u一次
而不是include
require_once
在出现错误时会抛出错误并终止脚本。这些错误应出现在错误日志文件中,该文件与包含的文件位于同一文件夹中include
只会发出警告并让脚本继续。我个人在任何地方都会使用require\u一次。另外,再次检查您的包含路径。您应该将$errorhandler传递给用户类。
像这样:
$user = new User($errorhandler);
在用户类中:
class User {
protected $err;
public function __construct($errorhandler) {
$this->err = $errorhandler;
//rest of your code
}
//rest of your code
}
或者简单地添加:global$errorhandler;在用户构造函数内部。我不明白为什么错误数组是空的。数组是公共的,我创建了类的一个对象。在我包含step1.php之后,错误(在测试打印中填充在User.php中)是空的。也许这是在Errorhandler类中为错误数组创建getter和setter的好方法?其实并不重要。我更喜欢在用户构造函数内的用户类(如我在示例中给出的)中进行修改,添加:global$errorhandler;但无论如何,我还是会选择我的答案:)