Reputation: 397
I have a problem with the accessing of classes.
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 Errorhandler();
$user = new User();
// Test
print_r($errorhandler->errors);
...html
Errorhandler.php
class Errorhandler {
public $errors = array();
...
}
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();
}
}
The index.php includes the relevant sections to build the site. In header.php I create two objects (1. errorhandler, 2. user). The User class check the return (boolean) from the sqlconnection. I know, that I use the wrong password and get a false. So the if ($conn) prints the print_r($errorhandler->errors) correctly. But when I want to show the errors in step1.php, the array errors is empty.
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>';
}
}
I tested it in header.php also and the errors array is empty too. So, the errors array is only filled in the User.php class, but I want to display the erros in step1.php. Is there a problem with the includes?
Edit: Hope to make it clearer:
header.php
// 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);
I set an error in class User:
$errorhandler->errors[] = "Can't connect to database.";
The array $errorhandler->errors is empty in header.php.
Upvotes: 0
Views: 67
Reputation: 17
You should pass $errorhandler to User class. Like this:
$user = new User($errorhandler);
And in User class:
class User {
protected $err;
public function __construct($errorhandler) {
$this->err = $errorhandler;
//rest of your code
}
//rest of your code
}
or simply add: global $errorhandler; inside User constructor.
Upvotes: 0
Reputation: 1118
Try using require_once
instead of include
. require_once
will throw an error when something goes wrong and kill the script. These errors should appear in an error_log file in the same folder as the file which has the include
in it. include
will only issue a warning and let the script continue. I personally use require_once
everywhere. Also, double check your include
paths.
Upvotes: 0
Reputation: 6793
You have to call a function. You cannot get value directly.
1st->$errorhandler = new Errorhandler();
2nd->Errorhandler.php
class Errorhandler {
function error() //create a function like this
{
$error= /*any error*/;
return $error;
}
}
3rd->
if ($err=$errorhandler->error()) {
foreach ($err as $error) {
echo '<div class="alert alert-danger message"><strong>Error:</strong>'.$error.'</div>';
}
}
Also try using require_once() instead of include();
Upvotes: 1