2

説明の仕方がわかりませんが、頑張ります。わかりました、私はこれらの3つのファイルを持っています:

  • Theme.php

    path: /shared/models/Theme.php
    class: Theme
    namespace: namespace models;
    
  • Custom.php

    path: /themes/default/Custom.php
    class: Custom
    namespace: this class does not use namespace
    
  • Settings.php

    path: /controllers/Settings.php
    class: Settings
    namespace: this class does not use namespace
    

私のようにSettings.php見えます:

<?php
class Settings
{
    public function apply()
    {
        $theme = new \models\Theme();
        $theme->customize(); // in this method the error is triggered
    }
}

Theme次に、以下のクラスを見てください。

<?php
namespace models;

class Theme
{
    public function customize()
    {
        $ext = "/themes/default/Custom.php";
        if (file_exists($ext))
        {
            include $ext;
            if (class_exists('Custom'))
            {            
                $custom = new Custom(); 
                //Here, $custom var in null, why???
            }
        }
    }
}

コードを実行すると、次のエラーが発生します。

Message: require(/shared/models/Custom.php) [function.require]: failed to open stream: No such file or directory
Line Number: 64

インタプリタがvarCustomで指定するのではなく、別のディレクトリからクラスをロードしようとするのはなぜですか?$ext

4

1 に答える 1

3

new Custom()名前空間のクラス内を呼び出すときは、\models実際にインスタンス化しようとしています\models\CustomCustomクラスには「名前空間がない」と言うので、new \Custom()代わりに試してください。

\models\Custom発生するエラーは、クラスファイルを要求しようとして失敗するクラスオートローダーから発生しているようです。

于 2013-01-14T02:35:57.353 に答える