waarom krijg ik een error
maar nu loop ik tegen een probleem aan ik krijg namelijk een error :
( ! ) Fatal error: Class 'config' not found in C:\wamp\www\index.php on line 4
Call Stack
# Time Memory Function Location
1 0.0015 239720 {main}( ) ..\index.php:0
ik heb gekeken maar ik zie niet wat ik fout doe als ik de tutorial ermee vergelijk
dit is de tutorial
https://www.youtube.com/watch?v=S6vDgLwJ7n8&index=3&list=PLfdtiltiRHWF5Rhuk7k4UAU1_yLAZzhWc#t=46.515
en dit is mijn index.php
en dit is mijn init.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
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
<?php
session_start();
$GLOBALS['config'] = array(
'mysql' => array(
'host' => '127.0.0.1',
'username' => 'root',
'password' => '',
'db' => 'testwebsite'
),
'remember' => array(
'cookie_name' => 'hash',
'cookie_expiry' => 604800
),
'session' => array(
'session_name' => 'user'
)
);
spl_autoload_register(function($class){
require_once 'classes/' . $class .'.php';
});
require_once 'functions/sanitize.php';
?>
session_start();
$GLOBALS['config'] = array(
'mysql' => array(
'host' => '127.0.0.1',
'username' => 'root',
'password' => '',
'db' => 'testwebsite'
),
'remember' => array(
'cookie_name' => 'hash',
'cookie_expiry' => 604800
),
'session' => array(
'session_name' => 'user'
)
);
spl_autoload_register(function($class){
require_once 'classes/' . $class .'.php';
});
require_once 'functions/sanitize.php';
?>
ziet iemand wat ik hier fout doe?
Gewijzigd op 11/02/2016 14:35:16 door Soccertime website
Lees mijn reactie in de YouTube comments :D.
Of hieronder:
Perhaps a better way to implement the config class would be thus:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
class Config {
public static function get($path) {
$config = $GLOBALS['config'];
$path = explode('/', $path);
foreach ($path as $bit) {
if (is_array($config) && array_key_exists($bit, $config)) {
$config = $config[$bit];
} else {
die('bit not found '.escape($bit)); // alternative: throw an exception
}
}
return $config;
}
}
?>
class Config {
public static function get($path) {
$config = $GLOBALS['config'];
$path = explode('/', $path);
foreach ($path as $bit) {
if (is_array($config) && array_key_exists($bit, $config)) {
$config = $config[$bit];
} else {
die('bit not found '.escape($bit)); // alternative: throw an exception
}
}
return $config;
}
}
?>
Explanation:
No default value for $path. It would simply not make sense not to supply a path.
No return false at the end. If no $path would be supplied we could falsely assume a value was successfully retrieved from the config because "false" might be a valid value!
array_key_exists instead of isset. Because if you would have null values in your config you would get a subarray as a return value because isset() evaluates to false on null values.
Dying on errors. If the path is incorrect, no meaningful things are returned from the config file. Because you do not know the impact of such a retrieval failure, you should just abort the execution of your application. This would also have been a great opportunity to introduce the OOP way of handling errors (try, catch, throw etc.). You could make an includes/500.php file for application errors such as this (failing to retrieve a configuration value).
Do not be afraid to let your application simply break when something goes wrong. If you try to patch it (for example by simply returning false) you might not even be aware that something is wrong!
Het bovenstaande fragment heeft ook gelijk deze volgende wijziging:
Edit: the line if (array_key_exists(...)) could also use an is_array() check in case you try to retrieve a non-existing key at the lowest level of a config path. For example mysql/host/nonexistent gives a warning without the is_array() check. Add this to the first part of the if-statement (hurray for lazy evaluation) like this: if (is_array($config) && array_key_exists($bit, $config)) { ... }
Al die andere reacties van het simpelweg retourneren van false zijn niet echt slim, want false zou een geldige configuratie-waarde kunnen zijn. Zoals aangegeven zou je dan dus onterecht kunnen concluderen dat het ophalen van de configuratie-variabele succesvol was.
-------------------------------------
EDIT: Wat in jouw geval mogelijk speelt: case-sensitive filesysteem? Je gebruikt config:: maar waarschijnlijk heet je bestand Config.php?
Gewijzigd op 11/02/2016 16:16:24 door Thomas van den Heuvel