Question

I would have used google but I'm going out and hope u guys can help me with a solution or tell me what I'm doing wrong by the time I get back.

I passed in an instance of my factory to my controller and based on something I want to use the factory to begin another instance of a controller that is related to this one bit I get

 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

I'm here is the factory and the controller I'm trying to call it from

factory

/* 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";
    }
}

The controller I'm trying to run it from:

     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());

sorry about the long question just wanted I gave you all you needed thanks in advance also if I'm doing something totally wrong please if you're gonna direct me to google at least give me something to google.

Was it helpful?

Solution

This line is recreating the IndexController very time.

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

When you create your index_controller via your object factory (line above) the new Index_Controller makes a call to varify_controller as this is defined within the __construct() (executed automatically when the Index_Controller is first instantiated).

varify_controller() contains yet another call to build_controller("index",$this->parameters); so we are then back to the beginning, endlessly creating new index controllers until you exceed the memory limit.

You can avoid these issues in the future by using the constructor for constructing the object. This should only include performing tasks that are need to get the object ready to be used, i.e. setting object properties or resolving object arguments.

OTHER TIPS

increase memory by addding the line at the top of your page

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top