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

  • Home
  • SEARCH
  • 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 639685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:52:12+00:00 2026-05-13T20:52:12+00:00

I want to write a simple class (PHP5) that can ‘run’ an unknown amount

  • 0

I want to write a simple class (PHP5) that can ‘run’ an unknown amount of subclasses. These subclasses are best translated as ‘checks’; they all will more or less do the same thing and give an answer (true / false). Think of it as system startup checks.

Over time new checks (subclasses) will be added to a directory and they should automatically be run when the main class is invoked. Others may write checks but they will have to follow the functions dictated by the main class.

What would be a clean yet simple way to build this?
I found the Factory pattern and combined with interface it seems a good starting point. However I am not sure, and I would appreciate advice on this.

EDIT: the provided answers here all work, however the answer by Gordon gives additional possibilities of batches and stacking, something I did not think of at the time of asking, but am now rather happy about.

  • 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-13T20:52:12+00:00Added an answer on May 13, 2026 at 8:52 pm

    If you want to create a Batch-like functionality, use the Command Pattern.

    Below is a very simple implementation of the pattern. The idea is to have a unified interface for all classes you want to invoke. Each class encapsulates one operation in the batch:

    interface BatchCommand
    {
        public function execute();
        public function undo();
    }
    

    One class implementing the interface will be the commander of all subclasses:

    class BatchCommander implements BatchCommand
    {
        protected $commands;
    
        public function add(BatchCommand $command)
        {
            $this->commands[] = $command;
        }
        public function execute()
        {
            foreach($this->commands as $command) {
                $command->execute();
            }
        }
        public function undo()
        {
            foreach($this->commands as $command) {
                $command->undo();
            }
        }
    }
    

    A simple Command could look like this:

    class FileRename implements BatchCommand
    {
        protected $src;
        protected $dest;
    
        public function __construct($src, $dest)
        {
            $this->$src;
            $this->dest;
        }
    
        public function execute()
        {
            rename($this->src, $this->dest);
        }
    
        public function undo()
        {
            rename($this->dest, $this->src);
        }
    }
    

    You could then use it like this:

    $commander = new BatchCommander;
    $commander->add(new FileRename('foo.txt', 'bar.txt'));
    $commander->add(/* other commands */);
    $commander->execute();
    

    Because BatchCommander is a BatchCommand itself, you can easily stack batches belonging together into other Batches, thus creating a very flexible tree structure, e.g.

    $batch1 = new BatchCommander;
    $batch1->add(/* some command */);
    $batch1->add(/* some other command */);
    
    $batch2 = new BatchCommander;
    $batch2->add(/* some command */);
    $batch2->add(/* some other command */);
    
    $main = new BatchCommander;
    $main->add($batch1);
    $main->add($batch2);
    $main->execute();
    

    In your Check/Test context, this means you could group conceptually belonging single tests into a test suite. And you can create test suites of test suites, by stacking one suite into another.

    Of course, you could also give the BatchCommander a file path to check on instantiation and have it init all BatchCommands by running through the files in the file path. Or pass it a Factory instance to use for creating the Check commands.

    You don’t have to have execute and undo for method names. Name it check if you want. Leave out undo if you don’t need it. The basic idea still stays the same though: one interface for all classes to be commanded, whatever that may look like. Feel free to adapt.

    An alternative with a somewhat different UseCase is the Chain of Reponsibility pattern. Check it out to see if this would be of utility too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 420k
  • Answers 420k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I would recommend shoulda if you want to stick with… May 15, 2026 at 10:43 am
  • Editorial Team
    Editorial Team added an answer Here's the answer, and it's a bit ugly, but hopefully… May 15, 2026 at 10:43 am
  • Editorial Team
    Editorial Team added an answer select new OfferType { name = offers.name, description = offers.description,… May 15, 2026 at 10:43 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.