0

我会使用谷歌,但我要出去,希望你们能帮助我解决问题,或者在我回来时告诉我我做错了什么。

我将工厂的一个实例传递给我的控制器,并基于我想使用工厂开始另一个与我得到的这一位相关的控制器实例

 Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 24 bytes) in C:\xampp\htdocs\includes\object_factory_inc.php on line 19

我在这里是工厂和我试图从中调用它的控制器

工厂

/* The purpose of this class is to create the instance of a class when needed and how needed.
        For example we could have created the controller object inline but in recognition
        of the polymorphic behavior of controllers we have the decided to create it in our object-
        factory class(this class) which serves to hide the logic of creating the correct instance of
        a specified controller. 

        For example the 'build_index_controller' function uses the second parameter
        of the parameters passed to it to decide on the instance of the index to call
        */


class object_factory_inc
{
    public function build_controller($controller_name,array $controller_params)
    {
        $indexed_controller_params=array_values($controller_params);
        $controller_name=strtolower($controller_name);


        if(!isset($indexed_controller_params[0]))
        {

            $controller_name=$controller_name."_controller";
            return new $controller_name($this,$controller_params);

        }

        else
        {
            $controller_build_function="build_".$controller_name."_controller";
            method_exists($this,$controller_build_function)?$this->$controller_build_function($controller_name,$controller_params,$indexed_controller_params):$this->controller_doesn't_exist($controller_build_function);
                }

            }

            private function build_index_controller($controller_name,array $controller_params,array $indexed_controller_params)
            {
                strtolower($indexed_controller_params[0]);
                switch ($indexed_controller_params[0])
                {
                    case "s":
                    $controller_name=$controller_name."_search_controller";
                    return new $controller_name($this,$controller_params);
                    break;

                    case "admin":
                    $controller_name=$controller_name."_admin_controller";
                    return new $controller_name($this,$controller_params);
                    break;

                    case "loggedin":
                    $controller_name=$controller_name."_loggedin_controller";
                    return new $controller_name($this,$controller_params);
                    break;

                    default://if it doesn't exist just run default controller behaviour consider running 404
            $controller_name=$controller_name."_controller";
            return $controller_name($this,$controller_params);
        }

    }


    private function controller_doesnt_exist($controller_name)
    {
        echo "404 error ".$controller_name." doesn't exist on this server";
    }
}

我试图从中运行它的控制器:

     class index_controller
    {

    protected $parameters=array();
    protected $default_operation='view';
    protected $object_factory;

    public function __construct(object_factory_inc $factory,array $parameters )
     {
        $this->object_factory=$factory;
        $this->parameters=$parameters;
        $this->varify_controller();
        //print_r($parameters);
        //var_dump($factory);
        //require_once "views/index_view.php";


      }

      private function varify_controller()
      {
          if(true)//if logged in session is set;
          {
              $indexd_parameteres=array_values($this->parameters);
              $indexd_parameteres[0]='loggedin';
              $this->object_factory->build_controller("index",$this->parameters);
          }
      }

    }

the normal way the controllers are usually called are from the index here it is

    require_once 'includes/config.php';


        $route=isset($_GET['action'])?new router_controller($_GET['action']):new router_controller($_GET['action']="/index");
        $route->parse_route();
        $object_factory=new object_factory_inc;
        $controller=$object_factory->build_controller($route->get_controller(),$route->get_request_data());

很抱歉,我只是想给你所有你需要的长问题,如果我做错了什么,请提前感谢,如果你要引导我去谷歌,至少给我一些谷歌的东西。

4

2 回答 2

1

这条线正在重新创造IndexController时间。

$this->object_factory->build_controller("index",$this->parameters);

当您index_controller通过对象工厂(上面的行)创建您的时, newIndex_Controller会调用它,varify_controller因为这是在 中定义的(在第一次实例化__construct()时自动执行)。Index_Controller

varify_controller()包含另一个调用,build_controller("index",$this->parameters);所以我们回到开始,无休止地创建新的索引控制器,直到超过内存限制。

您可以在将来通过使用构造函数来构造对象来避免这些问题。这应该只包括执行使对象准备好使用所需的任务,即设置对象属性或解析对象参数。

于 2013-10-11T19:34:32.303 回答
0

通过在页面顶部添加行来增加内存

ini_set('memory_limit', '512M');

于 2013-10-11T19:29:33.817 回答