• <tfoot id='ReQGA'></tfoot>
  • <i id='ReQGA'><tr id='ReQGA'><dt id='ReQGA'><q id='ReQGA'><span id='ReQGA'><b id='ReQGA'><form id='ReQGA'><ins id='ReQGA'></ins><ul id='ReQGA'></ul><sub id='ReQGA'></sub></form><legend id='ReQGA'></legend><bdo id='ReQGA'><pre id='ReQGA'><center id='ReQGA'></center></pre></bdo></b><th id='ReQGA'></th></span></q></dt></tr></i><div id='ReQGA'><tfoot id='ReQGA'></tfoot><dl id='ReQGA'><fieldset id='ReQGA'></fieldset></dl></div>
    1. <legend id='ReQGA'><style id='ReQGA'><dir id='ReQGA'><q id='ReQGA'></q></dir></style></legend>
        <bdo id='ReQGA'></bdo><ul id='ReQGA'></ul>

      <small id='ReQGA'></small><noframes id='ReQGA'>

      1. 具有动态类名的 PHP 命名空间

        PHP namespace with Dynamic class name(具有动态类名的 PHP 命名空间)
            <bdo id='ju0mH'></bdo><ul id='ju0mH'></ul>

            <small id='ju0mH'></small><noframes id='ju0mH'>

          • <legend id='ju0mH'><style id='ju0mH'><dir id='ju0mH'><q id='ju0mH'></q></dir></style></legend>

                <tbody id='ju0mH'></tbody>
              <tfoot id='ju0mH'></tfoot>
            1. <i id='ju0mH'><tr id='ju0mH'><dt id='ju0mH'><q id='ju0mH'><span id='ju0mH'><b id='ju0mH'><form id='ju0mH'><ins id='ju0mH'></ins><ul id='ju0mH'></ul><sub id='ju0mH'></sub></form><legend id='ju0mH'></legend><bdo id='ju0mH'><pre id='ju0mH'><center id='ju0mH'></center></pre></bdo></b><th id='ju0mH'></th></span></q></dt></tr></i><div id='ju0mH'><tfoot id='ju0mH'></tfoot><dl id='ju0mH'><fieldset id='ju0mH'></fieldset></dl></div>
                  本文介绍了具有动态类名的 PHP 命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  想知道其他人在使用 PHP 5.3 命名类的新功能时是否遇到过这个问题.

                  Wondering if anyone else has encountered this problem when utilizing the new ability to namespace classes using PHP 5.3.

                  我正在生成一个动态类调用,该调用利用一个单独的类在我的应用程序中定义用户类型.基本上,类定义器采用类型的整数表示并解释它们,返回一个包含类名的字符串,作为该用户的模型调用.

                  I am generating a dynamic class call utilizing a separate class for defining user types in my application. Basically the class definer takes an integer representation of types and interprets them, returning a string containing the classname to be called as the model for that user.

                  我有一个用户类型的对象模型,该名称在全局范围中定义,但我在 Editor 命名空间中有另一个同名的用户编辑器对象.出于某种原因,PHP 不允许我进行如下命名空间的动态调用.

                  I have an object model for the user's type with that name defined in the global scope, but I have another object with the same name for the user's editor in the Editor namespace. For some reason, PHP won't allow me to make a namespaced dynamic call as follows.

                  $definition = Definer::defineProfile($_SESSION['user']->UserType);
                  new Editor$definition();
                  

                  相同的语法适用于在全局命名空间中调用全局基本对象模型,我在整个应用程序中以这种方式可靠地使用它.

                  The identical syntax works for calling the global basic object model in the global namespace and I use it this way reliably throughout the application.

                  $definition = Definer::defineProfile($_SESSION['user']->UserType);
                  new $definition();
                  

                  这将正确调用动态所需的类.

                  This will correctly call the dynamically desired class.

                  是否有原因两者的行为会有所不同,或者由于这是一项新功能,因此该庄园尚未实现对命名空间的动态调用?是否有另一种方法可以从另一个命名空间动态调用一个类,而无需在代码中显式放置其名称,而是从一个变量中?

                  Is there a reason the two would behave differently, or has dynamic calling for namespaces not been implemented in this manor yet as this is a new feature? Is there another way to dynamically call a class from another namespace without explicitly placing its name in the code, but from within a variable?

                  推荐答案

                  好吧,把字符串中的命名空间拼出来就好了:

                  Well, just spell out the namespace in the string:

                  $definition = Definer::defineProfile($_SESSION['user']->UserType);
                  $class = '\Editor\' . $definition;
                  $foo = new $class();
                  

                  如果它是子命名空间(如注释中所示),只需在命名空间前面加上 __NAMESPACE__:

                  And if it's a child namespace (as indicated in the comments), simply prepend the namespace with __NAMESPACE__:

                  $class = __NAMESPACE__ . '\Editor\' . $definition;
                  

                  因此,如果当前命名空间是 FooBar,并且 $definition 是Baz",则生成的类将是 FooBarEditor巴兹

                  So if the current namespace is FooBar, and $definition is "Baz", the resulting class would be FooBarEditorBaz

                  这篇关于具有动态类名的 PHP 命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)

                  <tfoot id='UHBfm'></tfoot>
                • <legend id='UHBfm'><style id='UHBfm'><dir id='UHBfm'><q id='UHBfm'></q></dir></style></legend>
                  <i id='UHBfm'><tr id='UHBfm'><dt id='UHBfm'><q id='UHBfm'><span id='UHBfm'><b id='UHBfm'><form id='UHBfm'><ins id='UHBfm'></ins><ul id='UHBfm'></ul><sub id='UHBfm'></sub></form><legend id='UHBfm'></legend><bdo id='UHBfm'><pre id='UHBfm'><center id='UHBfm'></center></pre></bdo></b><th id='UHBfm'></th></span></q></dt></tr></i><div id='UHBfm'><tfoot id='UHBfm'></tfoot><dl id='UHBfm'><fieldset id='UHBfm'></fieldset></dl></div>

                      <bdo id='UHBfm'></bdo><ul id='UHBfm'></ul>

                            <tbody id='UHBfm'></tbody>
                        • <small id='UHBfm'></small><noframes id='UHBfm'>