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?)