-2

編集:

タイプミスを修正し、クラスの呼び出しは大文字と小文字を区別しません

私が持っている場合、私はそれを見ています

class A
{
    public function __construct()
    {
        echo 'hello';
    }
}

そしてこれをする

if (class_exists('a'))
    $class = 'a';

$a = new $class();

見ます

hellohello

私がコメントアウトした場合if statement、私は大丈夫です、それはechoアウトします

hello

class_exists()クラスコンストラクターの実行を停止するにはどうすればよいですか?

編集:

これが私の使い方です

foreach ($this->getNamespace() as $ns) {

                    //if (class_exists($ns . '\\' . $controller))
                        $controller = $ns . '\\' . $controller;

                    if (class_exists($ns . '\\' . $model))
                        $model = $ns . '\\' . $model;
                }

                $model = new $model($this->config);
                $controller = new $controller($this->config);
4

2 に答える 2

2

次のコードを実行する場合:

class A
{
    public function __construct()
    {
        echo 'hello';
    }
}

if (class_exists('a'))
    $class = 'a';

$a = new $class();

私は得る:

hello

あなたの問題は他の場所にある可能性があります。

于 2012-06-01T00:35:31.450 に答える
1

サンプル コードにいくつかのエラーがあります。これは正しいコードです:

<?php
class a // Class name is lower case
{
    public function __construct() // It's __construct not __constructor
    {
        echo 'hello';
    }
}

$class = 'stdClass';
if (class_exists('a')) { // Missing a closing parenthesis here
    $class = 'a';
}

$a = new $class();

これは以下を出力します:

hello

デモを見る

于 2012-06-01T00:23:11.267 に答える