实例化具有许多参数的类

I am creating my own MVC framework, just to learn something new and ran into this problem recently. Lets say I have like this:

Class Post extends \application\BaseClass
{
    private $objPostDetail = null;
    private $objAuthor = null
    private $objCategory = null;
    private $objType = null;
    private $objTitlePicture = null;

    public function __construct(PostDetail $objPostDetail,
                                Author $objAuthor, 
                                Category $objCategory, 
                                Type $objType, 
                                TitlePicture $objTitlePicture)
    {
        $this->objPostDetail = $objPostDetail;
        $this->objAuthor = $objAuthor;
        $this->objCategory = $objCategory;
        $this->objType = $objType;
        $this->objTitlePicture = $objTitlePicture;
    }
}

Then some objects used in the constuctor can also be comlex to create. I get data for it from PostDAO class, which returns array of data. Now the problem is how to create new instance of this class, since it may be on many places in the application. I think create everywhere $objAuthor, then $objCategory etc. to finally create $objPost is not good. So I created what I think may be called a Factory:

Class PostFactory extends \application\BaseFactory
{
    private $arrData = null;
    private $objPostDetail = null;
    private $objCategory = null;
    private $objType = null;
    private $objTitlePicture = null;

    public function __construct($arrData)
    {
        $this->arrData = $arrData;
    }

    public function build()
    {
        $this->objPostDetail = $this->buildPostDetail();
        $this->objCategory = $this->buildCategory();
        $this->objType = $this->buildType();
        $this->objTitlePicture = $this->buildTitlePicture();

        return $this->buildPost();
    }

    private function buildPostDetail()
    {
        $objPostDetail = new \includes\classes\factories\PostDetailFactory($this->arrData);
        return $objPostDetail->build();
    }

    private function buildCategory()
    {
        $objCategory = new \includes\classes\factories\CategoryFactory($this->arrData);
        return $objCategory->build();
    }

    private function buildType()
    {
        $objType = new \includes\classes\factories\TypeFactory($this->arrData);
        return $objType->build();
    }

    private function buildTitlePicture()
    {
        $objTitlePicture = new \includes\classes\factories\TitlePictureFactory($this->arrData);
        return $objTitlePicture->build();
    }

    private function buildPost()
    {
        return new \includes\classes\Post($this->objPostDetail, $this->objCategory,
                        $this->objType, $this->objTitlePicture);
    }
}

It works well, but I don't like that I have twice as much classes and I don't know what parameters do I need for instantiating Post since I pass array to the Factory class (because I want to avoid many parameters in the constructor). So, my question is what is the best way how to create an instance of class like this?

Thanks in advance for any help.

If you want to create your own MVC framework, I strongly suggest starting with some sort of "Container" which holds instances of service classes (classes that only have to be initialized once, for example Request or Response).

Use reflection classes to automatically inject the parameters a constructor needs by iterating over the function arguments of the constructor.

See the following example of an idea I usually use. It's very reliable and reasonably fast. If your framework has a LOT of different classes and you depend on this functionality a lot, I strongly recommend implementing some way of caching the parameter lists from the reflection classes.

<?php
class SomeClass
{
    public function __construct(Request $request, Response $response, $title = '')
    {
        echo get_class($request);
        // will output "Request"

        echo $title;
        // will output "Hello World"
    }
}

// This function will handle the dynamic dependency injection to make sure the constructor gets the arguments passed that it needs, with optional named arguments passing.
$some_class = YourFrameworkDispatcherClass->createInstance('SomeClass', array('title' => 'Hello World'));
?>

I've actually written a blog post about this on my blog. http://harold.info/engineering/php-dynamic-dependency-injection/

I think this can help you out with this structural problem.