areller
areller

Reputation: 5238

Something is wrong with class_alias - PHP

I'm trying to use class_alias on a facade \Facades\Security.

I tried this:

class_alias("\\Facades\\Security", "Security");
echo Security::Unique();

But i got an error (Class 'Security' not found in...).

The weird thing is that when i try this:

$facade = "Security";
class_alias("\\Facades\\" . $facade, $facade);
echo $facade::Unique();

It does work and i get an output from the function 'Unique'. So somehow storing the name of the facade in a variable and accessing it from there, fixes the problem...

Can someone tell me why?

Thanks :)

Upvotes: 0

Views: 1031

Answers (1)

Girish
Girish

Reputation: 12117

Suppose you are using namespace of class name.., so no need escape (\), try this code

class_alias("\Facades\Security", "Security");
echo Security::Unique();

Update

You will also need to add namespace in alias class B, see sample code

<?php
namespace Facades;

class Security {
 public static function Unique(){
   return "Test return";
 }
}

class_alias("\Facades\Security", "\Facades\B");

echo B::Unique();
?>

DEMO

Upvotes: 1

Related Questions