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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T12:20:36+00:00 2026-05-21T12:20:36+00:00

What is the best way to obtain the class name for a requested PHP

  • 0

What is the best way to obtain the class name for a requested PHP file if my unknown class follows the following pattern?

index.php

<?php
    require_once("startup.php");

    class NotIndex extends View
    {
        public function getView()
        {
            echo "<pre>View Data</pre>";
        }
    }
?>

For testing I want to create an instance of the NotIndex class within the startup.php script.

startup.php

<?php
    require_once("view.php");

    //Include the script again to bring in the class definition
    include($_SERVER['SCRIPT_FILENAME']);

    //Make sure I'm seeing any errors
    error_reporting(E_ALL);
    ini_set('display_errors','On');

    //If I knew the class I could just do this
    $Page = new NotIndex();
    $Page->getView();

    //Todo: Find the class from $_SERVER['SCRIPT_FILENAME']
    //Todo: Once we find the class create an instance and call getView()

    exit;
?>

view.php

<?php
    abstract class View
    {
        abstract function getView();
    }
?>

I’m thinking a regular expression solution might be simple since the desired data will always be between ‘class’ and ‘extends’. I don’t have much experience formulating the desired expression and would appreciate some insight.

The following solution may or may not be a good way of doing this, but it did lead me to a non-regular expression solution which is always a plus. If anyone can improve on this, then I will gladly give them credit for both my questions above. Credit actually goes to @Nik answering Stack Overflow question Extracting function names from a file (with or without regular expressions). If I do this in my startup script then I can probably count on the unknown class being the last one in the array. However, I plan on integrating this into my framework and would like a more complete solution.

Since the class name is unknown, you may ask how can we be sure? Well, the abstract class View comes right before the unknown class in the array. Any solution would surely have to check for this. So without guarantee the last class is the needed class, I probably need to traverse backwards in the array until I have identified the abstract View class and the previous value in the array would be the unknown subclass.

startup.php

<?php
    require_once("view.php");

    //Include the script again to bring in the class definition
    include($_SERVER['SCRIPT_FILENAME']);

    //Make sure I'm seeing any errors
    error_reporting(E_ALL);
    ini_set('display_errors','On');

    //If I knew the class I could just do this
    $Page = new NotIndex();
    $Page->getView();

    //Todo: Find the class from $_SERVER['SCRIPT_FILENAME']
    //Todo: Once we find the class create an instance and call getView()
    $Classes = get_declared_classes();
    $ViewObject = array_pop($Classes);

    $NewPage = new $ViewObject();
    $NewPage->getView();

    exit;
?>

View Data

Please expand any opinions on this usage.

So this solution only works if I include my abstract View class immediately before I include the unknown subclass script again via include($_SERVER['SCRIPT_FILENAME']);. So this implies the get_declared_classes() function adds the classes in the order they were defined or included. This could be a problem.

<?php
    require_once("view.php");

    //Include the script again to bring in the class definition
    include($_SERVER['SCRIPT_FILENAME']);
    include("killsolution.php");

    //Make sure I'm seeing any errors
    error_reporting(E_ALL);
    ini_set('display_errors','On');

    //If I knew the class I could just do this
    $Page = new NotIndex();
    $Page->getView();

    //Todo: Find the class from $_SERVER['SCRIPT_FILENAME']
    //Todo: Once we find the class create an instance and call getView()
    $Classes = get_declared_classes();
    $ViewObject = array_pop($Classes);

    $NewPage = new $ViewObject();
    $NewPage->getView();

    exit;
?>

Fatal error: Call to undefined method
killsolution::getView()


(This question is a subset of another open Stack Overflow question, What are some PHP object-oriented framework initialization techniques?)

  • 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-21T12:20:37+00:00Added an answer on May 21, 2026 at 12:20 pm

    The following solution allows you to iterate over classes defined at runtime and in my case search for a user defined subclass of View. The get_declared_classes() function will return both system and user defined classes in the order they were included. My system contained over 120 system classes before the requested script class was included. For this reason I iterate backwards in the returned array.

    startup.php

    //Include the script again to bring in the class definition
    include($_SERVER['SCRIPT_FILENAME']);
    
    //Make sure I'm seeing any errors for testing
    ini_set('display_errors','On');
    
    //Don't assume we will find a subclass of View
    $RequestClass = false;
    
    //Iterate over declared classes starting from the last defined class
    for($i = count($classes = get_declared_classes()) - 1; $i >= 0; $i--)
    {
        //Test if unknown class is a subclass of abstract View
        if(get_parent_class($classes[$i]) == 'View')
        {
            //Store the name of the subclass of View
            $RequestClass = $classes[$i];
    
            //We found our unknown View
            break;
        }
    }
    
    if($RequestClass)
    {
        //We can now instantiate the class or do something useful
        $Page = new $RequestClass();
        $Page->getView();
    }
    
    exit;
    

    Depending on when you call the get_declared_classes() function, the array could change. I know my unknown class will always be a subclass of View, therefore I can identify it no matter how many classes are defined afterwards. However, just to elaborate, if we have an unknown class that does not extend another class, then we can still obtain the last defined class using:

    $LastClass = array_pop(get_declared_classes());
    

    So we can always find the requested page’s class if we search for it immediately after it is defined. Theres alot of ways this will break, but most people I suspect will never need this functionality anyways.

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

Sidebar

Related Questions

I am wondering what the best way is using php to obtain a list
Whats the best/easiest way to obtain a count of items within an IEnumerable collection
What is the best way to verify/test that a text string is serialized to
What is the best way of creating an alphabetically sorted list in Python?
What is the best way to authorize all users to one single page in
What is the best way to store international addresses in a database? Answer in
What is the best way to include an html entity in XSLT? <xsl:template match=/a/node>
What is the best way to iterate through a strongly-typed generic List in C#.NET
What's the best way to handle translations for stock text in a SSRS .
What would be the best way to have a list of items with a

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.