waarom krijg ik een error

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Ventilatiesysteem Productontwikkelaar HBO WO Verwa

Samengevat: Zij bieden flexibele ventilatiematerialen, geluidsdempers, rookgasafvoer producten en industrieslangen. Ben jij een technisch productontwikkelaar? Heb jij ervaring met het ontwikkelen van nieuwe producten? Vaste baan: Technisch Productontwikkelaar HBO WO €3.000 - €4.000 Zij bieden een variëteit aan flexibele ventilatiematerialen, geluiddempers, rookgasafvoer producten, industrieslangen en ventilatieslangen voor de scheepsbouw. Met slimme en innovatieve materialen zorgen wij voor een gezonde en frisse leefomgeving. Deze werkgever is een organisatie die volop in ontwikkeling is met hardwerkende collega's. Dit geeft goede ontwikkelingsmogelijkheden. De branche van dit bedrijf is Techniek en Engineering. Functie: Voor de vacature als Technisch Productontwikkelaar Ede Gld HBO WO ga

Bekijk vacature »

Soccertime website

soccertime website

11/02/2016 14:32:07
Quote Anchor link
ik volg een tutorial playlist over hoe je een login/registratie functie op je website kunt invoegen
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

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
<?php
require_once 'core/init.php';

echo config::get('mysql/host'); //127.0.0.1

?>

en dit is mijn init.php

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
<?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
 
PHP hulp

PHP hulp

24/04/2024 11:25:15
 
Thomas van den Heuvel

Thomas van den Heuvel

11/02/2016 15:14:58
Quote Anchor link
Dat is best een aardige tutorial reeks.

Lees mijn reactie in de YouTube comments :D.

Of hieronder:
Perhaps a better way to implement the config class would be thus:

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
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;
    }
}

?>


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
 



Overzicht Reageren

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.