2012-05-07 16 views
2

私のディレクトリ構造はPHPの自動ロード

私は私だけで、適切な名前空間を(設定している

<?php 
.... // some other php code 
spl_autoload_register(NULL, FALSE); 
spl_autoload_extensions('.php'); 
spl_autoload_register(); 

classes\template::setTemplate('template/template1'); 
classes\template::setMaster('master'); 
.... // some other php code 
?> 

を使用していた私のconfig.phpファイル内

> Root 
> -Admin // admin area 
> --index.php // admin landing page, it includes ../config.php 
> -classes // all classes 
> ---template.php 
> ---template_vars.php // this file is used inside template.php as $template_vars = new tamplate_vars(); 
> -templates // all templates in different folder 
> --template1 
> -index.php 
> -config.php 

以下のようなものですクラス)とルート上の私のindex.phpに私はクラス

<?php 

require 'config.php'; 
$news_array = array('news1', 'news1'); // coming from database 

$indexTemplate = new \classes\template('index'); 
$indexTemplate->news_list = $news_array; // news_list variable inside index template is magically created and is the object of template_vars class 
$indexTemplate->render(); 
?> 

にアクセスこれまでのところ、それは完璧に働いている、それは一時をレンダリング後半と、

をテンプレートVARSを移入が、私は管理者フォルダ内のインデックスファイルを開いたとき、それは次のようなエラー

Fatal error: Class 'classes\template_vars' not found in /home/aamir/www/CMS/classes/template.php on line 47

にこの事を解決するためにどのように任意のアイデアを提供します。これは、ルートに動作しますが、管理パネルの内側から、それはあなたがそのためのトリックを使用する必要があり、作業

+0

どうかこのコードは私がそれを読むと痛いです。私はファイルを100 +編集したくない、と今では正常に動作しますので、先端のための – hakre

答えて

2

をdoesnot:

set_include_path(get_include_path() . PATH_SEPARATOR . '../'); 

あなたはconfig.php

インサイド ../config.php OR

set_include_path(get_include_path() . PATH_SEPARATOR . __DIR__ . '/'); 

が含まれる前に、

+0

おかげで、私はconfig.phpの内部 'set_include_path'を追加しました。 THANKX –

+0

ああ、 'config.php'の中で' __DIR__'を使っても、やってみましょう:) –

2

私は同じ問題(ウィンドウ上)に直面し、私はその問題を説明できると思います。例えば

はのは、2つの単純なスクリプトを見てみましょう、最初のものはrootと呼ばれるディレクトリにrootと呼ばれるnamespaceでクラスを作成します。

からroot\sampleClass.php

namespace root; 

class sampleClass 
{ 
    public function __construct() 
    { 
     echo __CLASS__; 
    } 

    public function getNameSpace() 
    { 
     return __NAMESPACE__; 
    } 
} 

、ここroot\index.phpに位置する第二のスクリプトその2つの行だけが含まれています:

spl_autoload_register(); 
$o = new sampleClass; 

(!) SCREAM: Error suppression ignored for 
(!) Fatal error: spl_autoload(): Class sampleClass could not be loaded in C:\wamp\www\root\test.php on line 4 

そして、あなたはroot\sampleClass.phpにnamespaceキーワード、エラーワニスを削除する場合:あなたはこのような致命的なエラーを取得しますroot\index.phpスクリプトを実行します。

私はこの起こるようなPHPのコアdevの何かはないですので、だから私は、コアPHPの態度について何を締結しません:あなたは、名前空間を指定しない場合spl_autoload_register();機能も見ていきます

現在のディレクトリ(root\)にあり、sampleClass.phpという名前のクラスを見つけますが、例でrootのような名前空間を指定すると、spl_autoload_register();関数は "root \ root"のような場所にファイルをロードしようとします。

だから、この時点で、あなたは2つの解決策があります:適切でない

1)最初はサブディレクトリを作成することでもsampleClass.php

2が含まれているrootと呼ばれる)優れている

set_include_path(get_include_path().PATH_SEPARATOR.realpath('..')); 

を教えてくれますどの:index.phpスクリプトでトリックの種類を追加することです親ディレクトリをチェックインする機能。

すべてです

関連する問題