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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T19:12:07+00:00 2026-05-12T19:12:07+00:00

I’m trying to do a bit of fancy/experimental php so that you can include

  • 0

I’m trying to do a bit of “fancy”/”experimental” php so that you can include a header script (called crt0 :-p) that defines a basic application class that you can inherit.

It then provides a function that checks your classes and finds one that is subclassing it’s application class and calls the “Main” function. This way you include the header, create a class that subclasses the default application class and ensure you provide a static main function and voila – you application magically picks up inside the main function (similar to java/c#, etc with lots of pre-defined application functionality).

The only issue I’m having is “waiting” until the users application class is defined; Because the include that defines the application class has to come before your user implementation the “check” is also being called within the same script and before the user defines their function so isn’t finding the users class as it hasn’t yet been defined.

The “trick” i was using was cuasing the application to run inside the register_shutdown_function function to then find the class and run it; It works fine as by this point all the classes have been defined but I’m not sure what state the system/script/process is in during this shutdown process and whether it’s safe to start running code here :-p.

Here’s an example of the code I had so far:

class Application {

    static $_instance = null;

    public static function getInstance(){
        if ( $this->_instance == null ){
            $this->_instance = new get_called_class();
        }
        return $this->_instance;
    }

}

function findApplicationClass(){
    $classes = get_declared_classes();
    foreach ( $classes as $class ){
        $c = new ReflectionClass ($class);
        if ( $c->isSubclassOf("Application")
        &&    $c->hasMethod("Main") ){
            call_user_func($class . '::Main');
        }
    }
}

function runApplication(){
    findApplicationClass();
}

register_shutdown_function('runApplication');

And then your application is simply:

include "../lib/com/crt0/crt0.php";

class MyApplication extends Application {

    static public function Main ( ){
        echo "Main function";
    }
}
  • 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-12T19:12:08+00:00Added an answer on May 12, 2026 at 7:12 pm

    You should use classes autoloading feauture instead of your include() to load them to avoid dirty tricks 😉

    • 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 have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string

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.