2017-04-18 2 views
2

I持って次のような構造どのようにsymfonyのクラスローダを使用する

API

共通

設定

ドキュメント

スクリプト

ベンダー

私はあなたが作曲ROOTDIR /ベンダーのフォルダの下にあるすべてのものを置く知っているかもしれませんが

<?php 
require_once BASE_DIR . 'vendor/autoload.php'; 
require_once BASE_DIR . 'vendor/symfony/class-loader/ClassLoader.php'; 

use Symfony\ClassLoader\ClassLoader; 

$loader = new \ClassLoader(); 

// to enable searching the include path (eg. for PEAR packages) 
$loader->setUseIncludePath(true); 

// ... register namespaces and prefixes here - see below 

$loader->register(); 

// register a single namespaces 
$loader->addPrefix('CCP', BASE_DIR . 'common/ccp/'); 


// cache files locations 
require_once BASE_DIR . 'vendor/symfony/class-loader/ApcClassLoader.php'; 

// sha1(__FILE__) generates an APC namespace prefix 
$cachedLoader = new ApcClassLoader(sha1(__FILE__), $loader); 

// register the cached class loader 
$cachedLoader->register(); 

// deactivate the original, non-cached loader if it was registered previously 
$loader->unregister(); 

が含まれている共通/ PHP/autoload.php下のカスタムオートローダーを置きます。

Error I get is Fatal error: Uncaught Error: Class 'ClassLoader' not found in ROOTDIR/common/php/autoload.php:7 

私は共通の下で私のカスタムクラスをロードしようとUPDATE/CCPは、それがロードされません。クラスの

Fatal error: Uncaught Error: Class 'CCP\Notification' not found in ROOTDIR/scripts/cli-test-email.php:12 

内容

<?php 
namespace CCP 

class Notification{ 
... 
} 

UPDATE 2

マイスクリプトscriptフォルダの下にあります。 useを追加しても、エラーは発生しませんが、エコーやエラーが発生しても何も起こりません。 useを削除すると、クラスNotificationが見つかりません。この

#!/opt/SP/php-7.0.10/bin/php -q 
<?php 
ini_set('display_startup_errors', 1); 
ini_set('display_errors', 1); 
error_reporting(E_ALL|E_STRICT); 
echo "before use"; 
use CCP/Notification; 
echo "after use"; 

$notification = new Notification(); 

print_r($notification); 
$notification->emailTest(); 
+0

'$ loader = new Symfony \ Component \ ClassLoader \ ClassLoader' – Federkun

+0

なぜ' Symfony \ Component \ ClassLoader \ 'をインクルードする必要がありますか? – shorif2000

+0

名前空間の使い方とpsr-0/4の意味 – Federkun

答えて

1

Indtead:

use Symfony\ClassLoader\ClassLoader; 

$loader = new \ClassLoader(); 

はあなたがいずれかを実行する必要があります。

$loader = new Symfony\ClassLoader\ClassLoader(); 

かを:

use Symfony\ClassLoader\ClassLoader; 

$loader = new ClassLoader(); //no backslash at the begining 

あなたはクラス名の前にバックスラッシュを入れて、それルート名前空間を意味するので、マットしませんあなたがここには使用されていないので、useを前に入れます。

例として、同じ名前の2つのクラスがあるとします。SomeClassそのうちの一つは、ルート名前空間の下にあり、もう一つは今Some/Namespace/SomeClass

の下にある:

use Some/Namespace/SomeClass; 

$object1 = new SomeClass(); //this is Some/Namespace/SomeClass instace 
$object2 = new \SomeClass(); //this is SomeClass from root namespace. 

EDIT:あなたの更新問題のあなたの問題のよう

- それはケースに関連する可能性があります感度。大文字と小文字の区別を含め、ディレクトリ名が名前空間に一致するようにしてください。

+0

common/ccpをcommon/ccpに変更しましたが、それでも同じ問題です – shorif2000

+0

更新を参照してください2 – shorif2000

+1

私はこれをうまく動作させることができませんでした。私はsymfonyの自動ロードが償却されているので、代わりに作者を使用しています。 jessesnet.com/development-notes/2014/php-composer-autoloading/ – shorif2000

関連する問題