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 1073319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:58:39+00:00 2026-05-16T20:58:39+00:00

Say I have a file Foo.php: <?php interface ICommand { function doSomething(); } class

  • 0

Say I have a file Foo.php:

<?php
interface ICommand
{
     function doSomething();
}

class Foo implements ICommand
{
    public function doSomething()
    {
        return "I'm Foo output";
    }
}

?>

If I want to create a class of type Foo I could use:

require_once("path/to/Foo.php") ;
$bar = new Foo(); 

But say that I’ve created a Chain of Command Pattern and I have a configuration file that registers all the possible classes and creates an instance of these classes based on strings that are present in the configuration file.

register("Foo", "path/to/Foo.php");

function register($className, $classPath)
{
    require_once($classPath); //Error if the file isn't included, but lets 
                              //assume that the file "Foo.php" exists.
    $classInstance = new $className; //What happens here if the class Foo isn't 
                                     //defined in the file "Foo.php"? 

    $classInstance->doSomething(); //And what happens here if this code is executed at
                                   //all?
    //Etc...
}

How do I make sure that these classes are actually where the configuration file says they are? And what happens if a class isn’t there (but the file is), will it create an instance of a dynamically generated class, that has no further description?

  • 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-16T20:58:40+00:00Added an answer on May 16, 2026 at 8:58 pm

    You can use class_exists to check if a class has been defined.

    If you are calling a class dynamically and also calling a method on that class from within the same function, you may also wish to call that method dynamically (unless all of your classes have the exact same method. If that is the case, you can also use method_exists

    Finally, you can also use file_exists to ensure the file can be included:

    register("Foo", "path/to/Foo.php", "bar", array('arg1', 'arg2'));
    
    function register($className, $classPath, $methodName, $args)
    {
        if(!file_exists($classPath)) return false;
    
        require_once($classPath);
    
        if(!class_exists($className)) return false;
    
        $classInstance = new $className;
    
        if(!method_exists($classInstance, $methodName)) return false;
    
        $classInstance->$methodName($args);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a really large file foo.txt and I want to iterate
Say I have a MATLAB object defined in a class file classdef foo properties
Let's say I have a foo.exe file that I want to use it from
Let's say I have a file located at C:\foo\bar.js and I want to include
say i have a file /var/www/foo/test.php how do i find out it's path from
I have a web server which returns an XML file. Lets say http://www.foo.bar/foo.php?wantXML=1 How
Say I have this small function in a source file static void foo() {}
Hi I have a php file at, say, localhost/foo/foo/bar.php which includes a file at
Say I have a binary file of 12GB and I want to slice 8GB
Say I have a data file that I want to process; I want to

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.