0

オープンソースの Web アプリケーションを実行しようとしています。ここから - https://sourceforge.net/projects/labshare-sahara/ Zend PHP フレームワークを使用します。

私の問題は、次のコードです。ファイルが見つからないため、false が返され、次のメッセージが返されます。

"2012-08-20T00:01:08+02:00 DEBUG (7): インクルード パスに認証クラス SAHARANS_Auth_Type_Database が見つかりません"

ログファイルに出力されます。


   private function _loadClass($name)
       {
            /* Find the class to load. */
            $file = implode('/', explode('_', $name)) . '.php';
            foreach (explode(PATH_SEPARATOR, get_include_path()) as $path)
            {
                $this->_logger->debug("does $path/$file exist?"); //I added this
                if (file_exists($path . PATH_SEPARATOR . $file))
                {
                    return new $name();
                }
            }

            $this->_logger->debug("Unable to find auth class $name in include path.");
            return false;
        }

もう少しコンテキストを提供するために、ログにさらに情報を出力するようにしました。そのため、for ループをラウンドするたびにログに書き込みます。これは出力です:

2012-08-20T00:47:07+02:00 DEBUG (7): does C:\xampp\htdocs\Sahara-WI\WI\application/../library/SAHARANS/Auth/Type/Database.php exist?

2012-08-20T00:47:07+02:00 DEBUG (7): does C:\xampp\htdocs\Sahara-WI\WI\library/SAHARANS/Auth/Type/Database.php exist?

2012-08-20T00:47:07+02:00 DEBUG (7): does C:\xampp\htdocs\Sahara-WI\WI\application\models/SAHARANS/Auth/Type/Database.php exist?

2012-08-20T00:47:07+02:00 DEBUG (7): does C:\xampp\htdocs\Sahara-WI\WI\institution/SAHARANS/Auth/Type/Database.php exist?

2012-08-20T00:47:07+02:00 DEBUG (7): does ./SAHARANS/Auth/Type/Database.php exist?

2012-08-20T00:47:07+02:00 DEBUG (7): does C:\xampp\php\PEAR/SAHARANS/Auth/Type/Database.php exist?

2012-08-20T00:47:07+02:00 DEBUG (7): Unable to find auth class SAHARANS_Auth_Type_Database in include path

2012-08-20T00:47:07+02:00 DEBUG (7): does C:\xampp\htdocs\Sahara-WI\WI\application/../library/Sahara/Auth/Type/Database.php exist?

2012-08-20T00:47:07+02:00 DEBUG (7): does C:\xampp\htdocs\Sahara-WI\WI\library/Sahara/Auth/Type/Database.php exist

2012-08-20T00:47:07+02:00 DEBUG (7): does C:\xampp\htdocs\Sahara-WI\WI\application\models/Sahara/Auth/Type/Database.php exist?

注意: Database.php は models\Sahara\Auth\Type ディレクトリに存在します!

すぐに奇妙に思えたのは、パス内の「/」と「\」の間の交換ですが、バックラッシュを強制しようとしても (私は Windows マシンを使用しています)、何の影響もないように見えました。

前もって感謝します!

4

3 に答える 3

1

PATH_SEPARATORは;です。Windowsの場合、および:他のオペレーティングシステムの場合。これは、(サブ)ディレクトリやファイル名ではなく、複数のパスを分割するために使用されます。

変化する:

if (file_exists($path . PATH_SEPARATOR . $file));

に:

if (file_exists($path . '/' . $file));

これはどこでも機能します。

于 2012-08-19T23:15:16.093 に答える
1

よく2つのこと:

  1. を使用し、使用DIRECTORY_SEPARATORしないでPATH_SEPARATORください。
  2. realpath()パスの異常を修正するために使用します。

誰かが興味を持っている場合は、私のオートローダーは次のようになります (ただし、ZF ではありません)。

<?php

namespace Application\Common;
/**
 * Autoloader class
 * This class will be responsible for autoloading of classes.
 */
class Autoloader {

    /** @var string $namespace  Namespace prefix for this instance */
    private $namespace;
    /** @var string $path       Path prefix for this instance */
    private $path;

    /**
     * @param string $namespace
     * @param string $path
     */
    public function __construct($namespace, $path) {
        $this->namespace = ltrim($namespace, "\\");
        $this->path      = rtrim($path, "/\\");
    }

    /**
     * Attempts to load a class.
     *
     * @param string $class The class name, including namespace.
     *
     * @return bool Success or fail based on class existence.
     */
    public function load($class) {
        $class = ltrim($class, "\\");
        //Check to see if class is from correct namespace defined by Autoloader instance.
        if (strpos($class, $this->namespace) === 0) {
            //Explode by namespace parts
            $namespace_parts = explode("\\", $class);
            //Class would be the last element, take it out and return it
            $class             = array_pop($namespace_parts);
            $namespace_parts[] = '';
            /* Build the directory path:
                The base path as defined in Autoloader
                The namespace parts, each a folder
                Parts of the class, separated by an underscore, become folders as well
            */
            $path = $this->path . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $namespace_parts);
            $path .= str_replace("_", DIRECTORY_SEPARATOR, $class) . ".php";
            $path = realpath($path);
            if (file_exists($path)) {
                require $path;
                return true;
            }
        }
        return false;
    }

    /**
     * Register the function.
     */
    public function register() {
        spl_autoload_register(array($this, "load"));
    }

    /**
     * Unregisters the function.
     */
    public function unregister() {
        spl_autoload_unregister(array($this), "load");
    }
}

使用する:

//Application being the common namespace prefix.
//__DIR__."Application" being the base directory.
$autoloader = new Application\Common\Autoloader("Application", __DIR__ . "Application");
$autoloader->register();
于 2012-08-19T23:18:36.097 に答える
0

オートローダーは実際にクラスで要求する必要があるため、おそらく次のようにします。

if (file_exists($path . PATH_SEPARATOR . $file))
{
    require $path . PATH_SEPARATOR . $file;
    return new $name();
}
于 2012-08-20T08:06:38.970 に答える