Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7644519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:42:34+00:00 2026-05-31T09:42:34+00:00

i’m quite new to php and trying to learn. I have 2 similiar classes.

  • 0

i’m quite new to php and trying to learn. I have 2 similiar classes. I want to create that objects when i pass count and object type. I read some about factory pattern. Here is my factory class:

    class AssetFactory
    {
        private static $table;
        public static $objects = array();
        public static function Create($asset,$count)
        {

            switch ($asset) {
            case "Item":
                self::$table = "items";
                break;
            case "Job":
                self::$table = "jobs";
                break;
            }
                $db = new Database();
                $rows = $db->query("SELECT * FROM ".self::$table." LIMIT ".$count);

                foreach($rows as $row)
                {
                    self::$objects[] =  new $asset($row);
                }

            return self::$objects;
         }
    }

and when i need 5 items i use:

  $myItems = AssetFactory::Create('Item',5);

when i need some jobs i use:

  $myJobs= AssetFactory::Create('Job',5);

item and job are that similiar classes. My question is here, as i said im trying to learn. Am i doing this right? Did i understand factory pattern right? Have any good documents about this(i read everything on php.net, got anything else).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T09:42:35+00:00Added an answer on May 31, 2026 at 9:42 am

    There are few thing that seem wrong in this case.

    First of all , there are two similar structures which are used for object creation:

    • factories: if object requires some sort of initialization before released for ‘consumption’
    • builders: if before creating object you have to create bunch of other objects

    Usually people do not distinguish between the two, and just call them “Factories”. So these would be two case where you use a factory.

    What you have right now does not fit the description. You are creating some sort of database connection, then getting some data, and then using it for creating a list of objects. This is not a reusable code.

    It would be much better if the usage of factory would be something like this :

    $connection = new PDO( .. blah.. );
    $stmt = $connection->query( 'SELECT * FROM '.$type.' LIMIT '.$count );
    $factory = new Factory;
    $collection = $factory->buildCollection( $type, $stmt->fetchALL(PDO::FETCH_ASSOC) );
    

    Of course, with factory class which implements this behavior.

    Additionally , you might want to watch (assuming, that you haven’t seen already) two videos on the subject:

    • Don’t Look For Things!
    • Unit Testing
    • Global State and Singletons
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I would like to count the length of a string with PHP. The string
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am trying to loop through a bunch of documents I have to put

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.