dongsui8162 2014-12-06 15:06
浏览 85

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

  • 写回答

1条回答 默认 最新

  • dongzhan5286 2014-12-06 15:12
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度