-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 回答