Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 13
  1. #1

    Come accedere ad una classa dentro un namespace?

    Perché non riesco a stampare $dns?

    Codice PHP:
    <?php

    namespace woo\controller {

        class 
    ApplicationHelper {

            function 
    getOptions() {
                if (!
    file_exists("data/woo_options_not_there.xml")) {
                    throw new \
    woo\base\AppException(
                    
    "Could not find options file");
                }
                
    $options simplexml_load_file("data/woo_options.xml");
                
    $dsn = (string) $options->dsn;
                print 
    $dns;
            }

        }

        
    $d = (new ApplicationHelper())->getOptions();
    }

    namespace 
    woo\base {

        class 
    AppException extends \Exception {
            
        }

    }
    ?>

    questo è l'errore:


    Fatal error: Uncaught exception 'woo\base\AppException' with message 'Could not find options file' in C:\xampp\htdocs\9781430260318_Chapter_12_Code\list ing12.00.php:9 Stack trace: #0 C:\xampp\htdocs\9781430260318_Chapter_12_Code\list ing12.00.php(19): woo\controller\ApplicationHelper->getOptions() #1 {main} thrown in C:\xampp\htdocs\9781430260318_Chapter_12_Code\list ing12.00.php on line 9
    Più pratica in futuro...

  2. #2
    Utente di HTML.it
    Registrato dal
    Feb 2014
    residenza
    Voltati
    Messaggi
    913
    cerchi di aprire un file inesistente
    No

  3. #3
    In C:\xampp\htdocs\9781430260318_Chapter_12_Code c'è listing12.00.php, lo script incriminato.

    In C:\xampp\htdocs\9781430260318_Chapter_12_Code\data c'è il file woo_options.xml.

    Non riesco proprio a capire...

    Questo è il file .xml:

    codice:
    <woo-options>
        <dsn>sqlite:./data/woo.db</dsn>
    
    <control>
        <view>main</view>
        <view status="CMD_OK">main</view>
        <view status="CMD_ERROR">error</view>
    
        <command name="ListVenues">
            <view>listvenues</view>
        </command>
    
        <command name="QuickAddVenue">
            <classalias name="AddVenue" />
            <view>quickadd</view>
        </command>
    
        <command name="AddVenue">
            <view>addvenue</view>
            <status value="CMD_OK"> 
              <forward>AddSpace</forward>
              <!--<forward>AddVenue</forward> -->
            </status>
        </command>
    
        <command name="AddSpace">
            <view>addspace</view>
            <status value="CMD_OK"> 
                <forward>ListVenues</forward>
            </status>
        </command>
    </control>
    </woo-options>
    Più pratica in futuro...

  4. #4
    Utente di HTML.it L'avatar di badaze
    Registrato dal
    Jun 2002
    residenza
    Lyon
    Messaggi
    5,372
    Scrivi $dsn e poi $dns.

    $options = simplexml_load_file("data/woo_options.xml");
    $dsn = (string) $options->dsn;
    print $dns;
    Ridatemi i miei 1000 posts persi !!!!
    Non serve a nulla ottimizzare qualcosa che non funziona.
    Cerco il manuale dell'Olivetti LOGOS 80B - www.emmella.fr

  5. #5
    Quote Originariamente inviata da badaze Visualizza il messaggio
    Scrivi $dsn e poi $dns.

    $options = simplexml_load_file("data/woo_options.xml");
    $dsn = (string) $options->dsn;
    print $dns;
    Uuuuuuu! Hai ragione, grazie! Ho provato a correggere ma ho ottenuto nuovamente questo:

    Fatal error: Uncaught exception 'woo\base\AppException' with message 'Could not find options file' in C:\xampp\htdocs\9781430260318_Chapter_12_Code\list ing12.00.php:9 Stack trace: #0 C:\xampp\htdocs\9781430260318_Chapter_12_Code\list ing12.00.php(19): woo\controller\ApplicationHelper->getOptions() #1 {main} thrown in C:\xampp\htdocs\9781430260318_Chapter_12_Code\list ing12.00.php on line 9


    NUOVO CODICE CON ERRORE:

    Codice PHP:
    <?php

    namespace woo\controller {

        class 
    ApplicationHelper {

            function 
    getOptions() {
                if (!
    file_exists("data/woo_options_not_there.xml")) {
                    throw new \
    woo\base\AppException(
                    
    "Could not find options file");
                }
                
    $options simplexml_load_file("data/woo_options.xml");
                
    $dsn = (string) $options->dsn;
                print 
    $dsn;
            }

        }

        
    $d = (new ApplicationHelper())->getOptions();
    }

    namespace 
    woo\base {

        class 
    AppException extends \Exception {
            
        }

    }
    ?>
    Più pratica in futuro...

  6. #6
    Una domanda:

    Nella lista di codici trovo anche questo file:

    C:\xampp\htdocs\9781430260318_Chapter_12_Code\woo\ controller\ApplicationHelper.php

    contenente al proprio interno una classe con questo codice:

    Codice PHP:
    namespace woo\controller;

    require_once( 
    'woo/base/Registry.php' );
    require_once( 
    'woo/base/Exceptions.php' );
    require_once( 
    'woo/controller/AppController.php' );

    class 
    ApplicationHelper {

    ...

         private function 
    getOptions() {

    ... 
    però quando apro questo:

    C:\xampp\htdocs\9781430260318_Chapter_12_Code\list ing12.00.php

    il file ApplicationHelper.php non viene assolutamente letto, giusto? (getOptions() andrebbero in conflitto... ...)

    Più pratica in futuro...

  7. #7
    Quote Originariamente inviata da badaze Visualizza il messaggio
    Scrivi $dsn e poi $dns.

    $options = simplexml_load_file("data/woo_options.xml");
    $dsn = (string) $options->dsn;
    print $dns;
    Su questo sono d'accordo ma il problema ora è l'eccezzione, cioè se scrivo questo:

    Codice PHP:
    <?php

    namespace woo\controller {

        
    // woo\controller\ApplicationHelper
        
    class ApplicationHelper {

            function 
    getOptions() {
                if (!
    file_exists("data/woo_options_not_there.xml")) {
                    print 
    "Non riesco ad aprire il file<br>";
                }
                
    $options simplexml_load_file("data/woo_options.xml");
                
    $dsn = (string) $options->dsn;
                print 
    $dsn;
            }

        }

        
    $d = (new ApplicationHelper())->getOptions();
    }

    namespace 
    woo\base {

        class 
    AppException extends \Exception {
            
        }

    }

    ?>

    funziona ma io voglio capire come chiamare AppException che si trova in un altro namespace...

    Più pratica in futuro...

  8. #8
    intanto sarebbe opportuno che ti mettessi una classe in un file e non usare la notazione namespace {}

    poi se vuoi indicare l'utilizzo di una classe presente in un altro namespace basta

    Codice PHP:
    namespace foo;

    use 
    bar\modules\foo;

    class 
    gimmi {

    Codice PHP:
    namespace bar\modules;

    use 
    PDO;

    class 
    foo {

    Questa volta, più che un voto.. è favoreggiamento.

  9. #9
    Quote Originariamente inviata da Al_katraz984 Visualizza il messaggio
    intanto sarebbe opportuno che ti mettessi una classe in un file e non usare la notazione namespace {}

    poi se vuoi indicare l'utilizzo di una classe presente in un altro namespace basta

    Codice PHP:
    namespace foo;

    use 
    bar\modules\foo;

    class 
    gimmi {

    Codice PHP:
    namespace bar\modules;

    use 
    PDO;

    class 
    foo {

    Eccomi ritornato, ho lavoricchiato un po' ed ora sono dinuovo qui a rompervi le palline. Premetto che preferisco adottare le parentesi perché il libro per comudità usa queste anche in esempi più avanti ma comunque credo non siano queste a dar fastidio. Su tuo consiglio ho provato a scrivere:

    a.php

    Codice PHP:
    <?php

    namespace woo\controller;

    use 
    woo\base;

    // woo\controller\ApplicationHelper
    class ApplicationHelper {

        function 
    getOptions() {
            if (!
    file_exists("data/woo_options_not_there.xml")) {

                
    $r = new \woo\base\AppException("Non riesco ad aprire il file<br>");
                throw new 
    $r;
            }
            
    $options simplexml_load_file("data/woo_options.xml");
            
    $dsn = (string) $options->dsn;
            print 
    $dsn;
        }

    }

    $d = (new ApplicationHelper())->getOptions();
    b.php

    Codice PHP:
    <?php

    namespace woo\base ;

        class 
    AppException extends \Exception {
            
        }
    ma ottengo questo errore:

    Fatal error: Class 'woo\controller\AppException' not found in C:\xampp\htdocs\9781430260318_Chapter_12_Code\a.ph p on line 13
    Più pratica in futuro...

  10. #10
    Com'è che nel messaggio di errore leggo:

    woo\controller\AppException

    mentre nel codice:

    woo\base\AppException

    "Mai discutere con un idiota. Ti trascina al suo livello e ti batte con l'esperienza." (Oscar Wilde)

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.