__construct上的语法错误,如果我使用ClassName作为构造函数签名,则有效

时间:2013-03-24 20:40:49

标签: php constructor syntax-error

当我创建此类的实例时,我收到错误

Parse error: syntax error, unexpected '__construct' (T_STRING), 
expecting variable    (T_VARIABLE) on line 9. 

这是我创建实例的方式:

    function controller_from_str($controller_name)
    {
        $controllers_namespace = 'MVCBP\\Controllers\\';
        $controller = $controllers_namespace 
            . $controller_name . 'Controller';

        return new $controller();
    }

如果我将__construct()更改为UserController(),则此功能正常。我在Windows 7上使用PHP 5.4这是预期的行为吗?

这是我的班级:

<?php
namespace MVCBP\Controllers;

use MVCBP\Core as Core;
use MVCBP\Models as Models;
use MVCBP\Repositories as Repos;

class UserController implements Core\ControllerInterface
{
    private $UserRepo;

    public function __construct()
    {
        $this->UserRepo = new Repos\UserRepository();
    }

    public function Index()
    {
        echo '<h1>User</h1>';
    }

    public function Create()
    {
        //TODO: if GET/POST
        $user = new Models\User();
        /*var_dump($user->getDb());*/
        render_view('User/Create');
    }

    private function CreateGet()
    {

    }

    private function CreatePost()
    {

    }
}

0 个答案:

没有答案