I have a php file which contains only one class. how can I know what class is there by knowing the filename? I know I can do something with regexp matching but is there a standard php way? (the file is already included in the page that is trying to figure out the class name).
To get the class name of an instance in Python: Use the type() function and __name__ to get the type or class of the Object/Instance. Using the combination of the __class__ and __name__ to get the type or class of the Object/Instance.
::class ¶ The class keyword is also used for class name resolution. To obtain the fully qualified name of a class ClassName use ClassName::class . This is particularly useful with namespaced classes.
Summary. Use the <select> element to create a dropdown list. Use the multiple attribute to create a list that allows multiple selections. Use $_POST to get the selected value of the select element if the form method is POST (or $_GET if the form method is GET ).
There are multiple possible solutions to this problem, each with their advantages and disadvantages. Here they are, it's up to know to decide which one you want.
This method uses the tokenizer and reads parts of the file until it finds a class definition.
Advantages
Disadvantages
Code
$fp = fopen($file, 'r'); $class = $buffer = ''; $i = 0; while (!$class) { if (feof($fp)) break; $buffer .= fread($fp, 512); $tokens = token_get_all($buffer); if (strpos($buffer, '{') === false) continue; for (;$i<count($tokens);$i++) { if ($tokens[$i][0] === T_CLASS) { for ($j=$i+1;$j<count($tokens);$j++) { if ($tokens[$j] === '{') { $class = $tokens[$i+2][1]; } } } } }
Use regular expressions to parse the beginning of the file, until a class definition is found.
Advantages
Disadvantages
echo "class Foo {";
)Code
$fp = fopen($file, 'r'); $class = $buffer = ''; $i = 0; while (!$class) { if (feof($fp)) break; $buffer .= fread($fp, 512); if (preg_match('/class\s+(\w+)(.*)?\{/', $buffer, $matches)) { $class = $matches[1]; break; } }
Note: The regex can probably be improved, but no regex alone can do this perfectly.
This method uses get_declared_classes()
and look for the first class defined after an include.
Advantages
Disadvantages
Code
$classes = get_declared_classes(); include 'test2.php'; $diff = array_diff(get_declared_classes(), $classes); $class = reset($diff);
Note: You cannot simply do end()
as others suggested. If the class includes another class, you will get a wrong result.
This is the Tokenizer solution, modified to include a $namespace variable containing the class namespace, if applicable:
$fp = fopen($file, 'r'); $class = $namespace = $buffer = ''; $i = 0; while (!$class) { if (feof($fp)) break; $buffer .= fread($fp, 512); $tokens = token_get_all($buffer); if (strpos($buffer, '{') === false) continue; for (;$i<count($tokens);$i++) { if ($tokens[$i][0] === T_NAMESPACE) { for ($j=$i+1;$j<count($tokens); $j++) { if ($tokens[$j][0] === T_STRING) { $namespace .= '\\'.$tokens[$j][1]; } else if ($tokens[$j] === '{' || $tokens[$j] === ';') { break; } } } if ($tokens[$i][0] === T_CLASS) { for ($j=$i+1;$j<count($tokens);$j++) { if ($tokens[$j] === '{') { $class = $tokens[$i+2][1]; } } } } }
Say you have this class:
namespace foo\bar { class hello { } }
...or the alternative syntax:
namespace foo\bar; class hello { }
You should have the following result:
var_dump($namespace); // \foo\bar var_dump($class); // hello
You could also use the above to detect the namespace a file declares, regardless of it containing a class or not.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With