Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List of all PHP Aliases

Tags:

alias

php

In PHP, you can define a class alias using the class_alias function. Is there a way to get a list of all the class aliases currently defined at runtime?

like image 310
Alan Storm Avatar asked Sep 20 '14 06:09

Alan Storm


2 Answers

As hek2mgl shows this is not possible in PHP. Depending on why you want this there might be viable workarounds though.

I am assuming right now you are trying to detect aliases created in 3rd party code. You could then install the Runkit module, set runkit.internal_override to 1 to enable native function modification, and do something like this in the root file of your project:

runkit_function_rename('class_alias', 'class_alias_internal');
function class_alias($original, $alias, $autoload = TRUE)
{
    Logsomewhere("Creating alias from $original to $alias!");
    class_alias_internal($original, $alias, $autoload);
}

Of course this also means that you could build exactly the list you're seeking. I don't see any runtime production use for this functionality (correct me if I'm wrong) so you'd only have to do this on a development server for as long as needed. As Runkit is quite a dangerous module I would even disable it on a devserver as soon as you're done with it.

For other scenarios similar workarounds might be viable, but I'd need to know why you seek this information. As far as PHP is concerned an alias is supposed to be undetectable, so it's doing its job fine (for a change).

like image 176
Niels Keurentjes Avatar answered Nov 12 '22 21:11

Niels Keurentjes


Had a look at PHP's source code. After the alias has been registered, PHP doesn't know about what is the original and what is the alias. Like hardlinking in file system. You can view the code, in it's most recent version, here: https://github.com/php/php-src/blob/master/Zend/zend_API.c#L2728

(note that the link might be outdated at the moment you are reading this. search for the function zend_register_class_alias_ex then.)

However, a function that shows entries of the class table having more than one named reference to it could be crafted (in PHP's core, with C) but it looks like it isn't available at the moment.

like image 44
hek2mgl Avatar answered Nov 12 '22 23:11

hek2mgl