Variable functions with namespaces in PHP

前端 未结 4 2250
孤街浪徒
孤街浪徒 2021-02-14 02:58

I\'m wondering if there is a way to call variable functions with namespaces. Basically I\'m trying to parse tags and send them to template functions so they can render html`

4条回答
  •  眼角桃花
    2021-02-14 03:34

    try with

     // Main php file
    require_once 'template.php';
    foreach (array("javascript","script","css") as $tag) {
        call_user_func("template\\$tag"); // As of PHP 5.3.0
    }
    
     // template.php
     namespace template;
    
     function javascript() { return "Hello from javascript"; }
     function css() { return "Hello from css"; }
     function script() { return "Hello from script"; }
    

    you have some info here

提交回复
热议问题