4

私のディレクトリ構造は以下のようなものです

> 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

私のconfig.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
?>

適切な名前空間を設定し(クラスのみ)、ルートの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 を入力します。

しかし、管理フォルダーのインデックスファイルを開くと、次のエラーが発生します

致命的なエラー: クラス 'classes\template_vars' が /home/aamir/www/CMS/classes/template.php の 47 行目に見つかりません

このことを修正する方法はありません。root では動作しますが、管理パネル内からは動作しません

4

2 に答える 2

5

そのためには、トリックを使用する必要があります。

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

ORを含める前に../config.php

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

中身config.php

于 2012-05-07T09:51:42.453 に答える
2

私は(Windowsで)同じ問題に直面しましたが、その問題を説明できると思います。

たとえば、2 つの単純なスクリプトを考えてみましょう。最初のスクリプトは、呼ばれるディレクトリに呼び出されnamespaceたクラスを作成します。rootroot

- root\sampleClass.php:

namespace root;

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

    public function getNameSpace()
    {
        return __NAMESPACE__;
    }
}

ここにある2番目のスクリプトには、次のroot\index.php2行のみが含まれています。

spl_autoload_register();
$o = new sampleClass;

スクリプトを実行すると、root\index.php次のような FATAL エラーが発生します。

( ! ) 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と、エラーは消えます。

私はphpコア開発者ではないので、コアphpの態度については何も結論付けませんが、次のようなことが起こります:

名前空間を指定しない場合、spl_autoload_register();関数は現在のディレクトリ ( root\) も検索し、呼び出されたクラスを見つけますが、この例のsampleClass.phpように名前空間を指定すると、関数は「root\」のような場所にあるファイルをロードしようとします。根"。rootspl_autoload_register();

したがって、この時点で 2 つの解決策があります。

1)最初に適切ではないのは、というサブディレクトリを作成することですrootsampleClass.php

2)index.php 2 つ目は、スクリプトに一種のトリックを追加することです。

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

spl_autoload_register()親ディレクトリをチェックインするように関数に指示します。

それで全部です

于 2012-09-23T18:44:14.023 に答える