Class aanroepen in een andere class
De bedoeling is, als er iets misloopt in news.php, dat errors.php ervoor zorgt dat er een gepaste error komt, ik wil nu de klasse van errors.php aanroepen in de klasse van news.php, maar dit lukt me niet, hoe moet ik dit doen zodat ik aan de functies uit de klasses van errors.php kan?
ps: ik ben oop ant leren vandaar er soms "stomme fouten" kunnen inzitten :p
news.php
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php
require_once('errors.php');
$ep = new errors;
class news
{
var $db = '';
var $host = "";
var $user = "";
var $password = "";
function __mysql_connect()
{
@mysql_connect($this->host,$this->user,$this->password) or $ep-> __print_error('boe');
}
function __get_data()
{
}
function __output()
{
}
}
$a=new news;
$a->__mysql_connect();
?>
require_once('errors.php');
$ep = new errors;
class news
{
var $db = '';
var $host = "";
var $user = "";
var $password = "";
function __mysql_connect()
{
@mysql_connect($this->host,$this->user,$this->password) or $ep-> __print_error('boe');
}
function __get_data()
{
}
function __output()
{
}
}
$a=new news;
$a->__mysql_connect();
?>
errors.php
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
class errors
{
function __print_error($message)
{
$error = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">';
$error .= '<html>';
$error .= ' <head>';
$error .= ' <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />';
$error .= ' <title>Error</title>';
$error .= ' </head>';
$error .= ' <body>';
$error .= ' <h1>An error occured!</h1> <br>';
$error .= ' ' . $message . '<br>';
$error .= ' <i>If this is the first time that you\'ve got the error, please reload this page by pressing the <strong>F5</strong> button or the <strong>refresh button</strong> of your browser.<br> Else contact the webmaster with this message included, please note wat you\'re doing and where you are!</i>';
$error .= ' </body>';
$error .= '</html>';
die($error);
}
}
?>
class errors
{
function __print_error($message)
{
$error = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">';
$error .= '<html>';
$error .= ' <head>';
$error .= ' <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />';
$error .= ' <title>Error</title>';
$error .= ' </head>';
$error .= ' <body>';
$error .= ' <h1>An error occured!</h1> <br>';
$error .= ' ' . $message . '<br>';
$error .= ' <i>If this is the first time that you\'ve got the error, please reload this page by pressing the <strong>F5</strong> button or the <strong>refresh button</strong> of your browser.<br> Else contact the webmaster with this message included, please note wat you\'re doing and where you are!</i>';
$error .= ' </body>';
$error .= '</html>';
die($error);
}
}
?>
Waarom staat je error functie in een class?
Je kan, om de error class in de news class altijd beschikbaar te hebben, in de news class constructor een instance creeren van de error class en die opslaan in het news object (logisch toch? :)):
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
<?php
class news
{
function news()
{
require_once('errors.php');
$this->errors = new errors;
}
}
?>
class news
{
function news()
{
require_once('errors.php');
$this->errors = new errors;
}
}
?>